?
Solved

What does this regx do?  ?:

Posted on 2006-10-25
4
Medium Priority
?
208 Views
Last Modified: 2010-03-05
What does this expression do ... with a some explanation please.  Thanks.

    $txt =~ s{
         (
                         [^"'/]+
                 |
                         (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
                 |
                         (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
         )
 |
         / (?:
                 \*[^*]*\*+(?:[^/*][^*]*\*+)*/
                 |
                 /[^\n]*
           )
     }{$1}gsx;
0
Comment
Question by:hank1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17808011
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(
qr{
         (
                         [^"'/]+
                 |
                         (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
                 |
                         (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
         )
 |
         / (?:
                 \*[^*]*\*+(?:[^/*][^*]*\*+)*/
                 |
                 /[^\n]*
           )
}sx)->explain;


The regular expression:

(?sx-im:
         (
                         [^"'/]+
                 |
                         (?:"[^"\\]*(?:\\.[^"\\]*)*" [^"'/]*)+
                 |
                         (?:'[^'\\]*(?:\\.[^'\\]*)*' [^"'/]*)+
         )
 |
         / (?:
                 \*[^*]*\*+(?:[^/*][^*]*\*+)*/
                 |
                 /[^\n]*
           )
)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?sx-im:                 group, but do not capture (with . matching
                         \n) (disregarding whitespace and comments)
                         (case-sensitive) (with ^ and $ matching
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^"'/]+                  any character except: '"', ''', '/' (1
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?:                      group, but do not capture (1 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      "                        '"'
----------------------------------------------------------------------
      [^"\\]*                  any character except: '"', '\\' (0 or
                               more times (matching the most amount
                               possible))
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        \\                       '\'
----------------------------------------------------------------------
        .                        any character
----------------------------------------------------------------------
        [^"\\]*                  any character except: '"', '\\' (0
                                 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      "                        '"'
----------------------------------------------------------------------
      [^"'/]*                  any character except: '"', ''', '/' (0
                               or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    )+                       end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?:                      group, but do not capture (1 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
      [^'\\]*                  any character except: ''', '\\' (0 or
                               more times (matching the most amount
                               possible))
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        \\                       '\'
----------------------------------------------------------------------
        .                        any character
----------------------------------------------------------------------
        [^'\\]*                  any character except: ''', '\\' (0
                                 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
      [^"'/]*                  any character except: '"', ''', '/' (0
                               or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    )+                       end of grouping
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \*                       '*'
----------------------------------------------------------------------
    [^*]*                    any character except: '*' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \*+                      '*' (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      [^/*]                    any character except: '/', '*'
----------------------------------------------------------------------
      [^*]*                    any character except: '*' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      \*+                      '*' (1 or more times (matching the
                               most amount possible))
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
    /                        '/'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    /                        '/'
----------------------------------------------------------------------
    [^\n]*                   any character except: '\n' (newline) (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 
LVL 84

Accepted Solution

by:
ozo earned 1000 total points
ID: 17808045
see
perldoc -q "C style comments"
0
 
LVL 1

Author Comment

by:hank1
ID: 17811364
If there is a (?: ) match, and its the only match, then $1
substitutes an empty string.  So.... a match in the second
main group deletes the match.

In the first main group only the match for [^"'/]+ saves.  The
other two alternative groups begin with (?:, so nothing they
hit gets saved.  Correct?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17812983
(?: ) does not capture
(    ) captures
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question