Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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.


Comment on Re: What this pattern matches
Download Code
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2016-05-26 03:31 GMT
Find Nodes?
    Voting Booth?