Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

quick question about parenthesis and regular expressions

by cranberry13 (Beadle)
on Nov 04, 2003 at 09:47 UTC ( [id://304360]=perlquestion: print w/replies, xml ) Need Help??

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

Hello there!

I have a quick question about using regular expresssions.

I want to match

 $word =~ /absorb|absorbs|absorbed/i

and save the match in $1 (and supposing there can only be one match), how do I place the 'parenthesis'?

This doesn't work:

 $word =~ /(absorb|absorbs|absorbed)/i

What am I doing wrong?

Replies are listed 'Best First'.
Re: quick question about parenthesis and regular expressions
by Enlil (Parson) on Nov 04, 2003 at 09:58 UTC
    But it DOES work. If $word contains "absorb" in it at all $1 will be set to absorb, after the match.

    One thing of note is that $1 will never match absorbs nor absorbed, as as it will match absorb first, the regex engine will consider the regex a success and stop. if on the other hand you want to match absorbed or absorbs you might want to change the order of the alternation around, so that the last thing it attempts to match is absorb. or for less backtracking you might try something like:

    $word =~ /(absorb(?:s|ed)?)/i; print $1;

    updated:added the ? as per ysth below. ysth++ for catching my oversight.

    -enlil

      You need to say (?:s|ed)? or "absorb" won't match.
      Enlil--
      Thanks for the reg expression -- it works :)

      But here's an interesting problem, it seems if i'm using a regular expression with the substitution operator, the value of $1 comes up blank.

      Why is that?

      What I really want to do is this:

      if ($line =~ s/($word(?:s|ed))/<b>$1<\/b>/igm)

      assuming that:

      $line= "She was very absorbed in her homework."; $word = "absorb";

      I want it to match absorbed and but store the value of it in $1 so that I can use it later.

        Ummm, I have no problem doing the substitution using the code supplied:

        $line = "She was very absorbed in her homework."; $word = "absorb"; print STDOUT "Line: $line\n"; $line =~ s/($word(?:s|ed))/<b>$1<\/b>/igm; print STDOUT "New Line: $line\n"; print STDOUT "\$1 contains: $1\n";

        Of course, I might consider changing it slightly to read:

        $line =~ s/\b(${word}(?:s|ed)?)\b/<b>$1<\/b>/igm;

        However, I did just discover that there is something more going on here, because while the original (without the \b) does set something in $1, my change doesn't (despite doing the substitution properly).

        And finally, if you're just doing a match why are you using s///?

Re: quick question about parenthesis and regular expressions
by PodMaster (Abbot) on Nov 04, 2003 at 09:55 UTC
    Define "work" because according to my definition, it does ;)
    for my $word ( qw[ absorb absorbo absorbs absorbed absorbarararara ]){ $word =~ /(absorb|absorbs|absorbed)/i and warn " $1 "; } __END__ absorb at - line 2. absorb at - line 2. absorb at - line 2. absorb at - line 2. absorb at - line 2.
    Perhaps you wish to investigate some anchors (namely ^ and $). Also of interest might be (besides the manual pages) YAPE::Regex::Explain.

    update: Enlils response reminded me of Regex::PreSuf

    use Regex::PreSuf; my $re = presuf(qw(absorb absorbs absorbed absorbing )); die $re; __END__ absorb(?:ed|ing|s)? at - line 3.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found