Solved

What does this regx do?  ?:

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 130
perl rename 2 142
Extract multiple value with delimiters from a string 4 168
Validating the data using Oracle DBD module. 5 65
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 …
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now