Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What does this regx do?  ?:

Posted on 2006-10-25
4
Medium Priority
?
211 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

604 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