Solved

What does this regx do?  ?:

Posted on 2006-10-25
4
167 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
  • 3
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
see
perldoc -q "C style comments"
0
 
LVL 1

Author Comment

by:hank1
Comment Utility
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
Comment Utility
(?: ) does not capture
(    ) captures
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now