Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: What this pattern matches

by Enlil (Parson)
on May 12, 2006 at 05:09 UTC ( #548905=note: print w/ replies, xml ) Need Help??


in reply to What this pattern matches

With a little YAPE::Regex::Explain magic:

$ perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/^ +(.+)_[^_]+$/)->explain' The regular expression: (?-imsx:^(.+)_[^_]+$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- _ '_' ---------------------------------------------------------------------- [^_]+ any character except: '_' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

or a little more tersely. Match any string that contains at least one character before and one character after the last underscore that appears in the string.

-enlil


Comment on Re: What this pattern matches
Download Code
Re^2: What this pattern matches
by zerogeek (Monk) on May 12, 2006 at 08:22 UTC
    First, let me say that this is one of the best explanations of a regex that I have seen.

    Now, can anyone give a good example of 1 or 2 lines that would match and 1 or 2 that would NOT match?

    Allow me to reword and provide a possible match example:
    Match a line with at least one underscore (if more than one, the following applies ONLY to the LAST underscore) that is both preceeded AND followed by at least one character.

    Match: I_love
    Match: I_love_perl
    Match: I_love_perl_!
    No Match: I_love_perl!_

    Am I right?

Re^2: What this pattern matches
by mikeock (Hermit) on May 12, 2006 at 15:37 UTC
    What a neat module. I was un-aware of this until seeing this post!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://548905]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-22 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (156 votes), past polls