Solved

What does this regx do?  ?:

Posted on 2006-10-25
4
199 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 250 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

696 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