Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

RegExp Matched Values

by rpike (Scribe)
on Apr 30, 2010 at 13:22 UTC ( #837764=perlquestion: print w/ replies, xml ) Need Help??
rpike has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a regular expression in place that does a check on a string coming in from a form. The regular expression is as such : $string =~ m/^(frm)(\w+)(txt)?$/; What I was hoping to accomplish was to have $1, $2, and $3 populated with the values in the brackets. When I run this and the string is something like frmNametxt the $1 gets set to frm, $2 gets set to Nametxt and $3 to nothing. What (if anything) can I do to force the code to evaluate and store txt into $3? If txt is there I want it put into $3. Any help would be most appreciated. Thanks. Rob

Comment on RegExp Matched Values
Re: RegExp Matched Values
by AR (Friar) on Apr 30, 2010 at 13:30 UTC

    In this very specific example, you can change the \w+ into the lazy version: \w+?. If you're looking for a solution to a more general problem, this may not be appropriate.

Re: RegExp Matched Values
by jethro (Monsignor) on Apr 30, 2010 at 13:37 UTC
    Use $string =~ m/^(frm)(\w+?)(txt)?$/;. This works because \w+? tries to match the smallest possible string now

    Note this works only because the string is anchored at the end. Otherwise \w+? would always match only one character and (txt)? the empty string.

      $string =~ m/^(frm)(\w+?)(txt)?$/;

      There's a problem here in that if  'txt' may not be present (as  (txt)? implies) and an overall match is still desired,  $3 will be undefined because the third capture group need not match at all. A different approach will yield an empty string instead.

      >perl -wMstrict -le "for (@ARGV) { m/^(frm)(\w+?)(txt)?$/; print qq{'$_' ($1) ($2) ($3)}; } print '-----------'; for (@ARGV) { m/^(frm)(\w+?)(txt|)$/; print qq{'$_' ($1) ($2) ($3)}; } " frmNametxt frmNameFoo 'frmNametxt' (frm) (Name) (txt) Use of uninitialized value $3 in concatenation (.) or string at ... 'frmNameFoo' (frm) (NameFoo) () ----------- 'frmNametxt' (frm) (Name) (txt) 'frmNameFoo' (frm) (NameFoo) ()

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-08-21 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (134 votes), past polls