Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

What this pattern matches

by Anonymous Monk
on May 12, 2006 at 04:56 UTC ( [id://548901]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: What this pattern matches
by Enlil (Parson) on May 12, 2006 at 05:09 UTC
    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

      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?

      What a neat module. I was un-aware of this until seeing this post!
Re: What this pattern matches
by GrandFather (Saint) on May 12, 2006 at 05:11 UTC

    It matches and captures at least one character and as any as it can up to (but not including) the last underscore in a string. It fails if there is not an underscore in the string and if there is not at least one character before and after the underscore.


    DWIM is Perl's answer to Gödel
Re: What this pattern matches
by ikegami (Patriarch) on May 12, 2006 at 05:22 UTC

    That code stores in $1 a copy of $b with everything after the last "_" removed, including the "_" itself.

    There are exceptions.
    $1 is left unchanged if $b contains no "_".
    $1 is left unchanged if $b contains nothing before the last "_".
    $1 is left unchanged if $b contains nothing after the last "_".

    When executed in list context, it returns $1 on match or an empty list on failure to match.

Re: What this pattern matches
by gopalr (Priest) on May 12, 2006 at 05:15 UTC
    $b='This_is_Sample_Text'; if ($b=~/^(.+)_[^_]+$/) { print "\n$1"; ## This_is_Sample }

    . = Match any character (except newline)

    + = Match 1 or more times

    .+_ = Match upto last underscore(_)

    [^_]+$ = Notmatch underscore(_) upto end of the string

Re: What this pattern matches
by rozallin (Curate) on May 12, 2006 at 10:40 UTC
Re: What this pattern matches
by Jasper (Chaplain) on May 12, 2006 at 10:21 UTC
    look_up_the_documentation?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://548901]
Approved by Errto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found