Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: regex capturing problem

by larryk (Friar)
on Mar 21, 2002 at 13:43 UTC ( #153317=note: print w/ replies, xml ) Need Help??

in reply to regex capturing problem

because your RE is looking for a "word" (alphanum chars) ending in an ' followed by a word (with possible hyphens) immediately followed by the end of line. this is not the case for "d'aujourd" as there is no end of line. the RE skips this and moves on to "ajourd'hui" which is followed by end of line - ergo a match.

what I believe you want to do is match all wordparts followed by an ' or the end of line like so:

@words = $word =~ /(\w+)['\z]?/g;
perl -le "s,,reverse killer,e,y,rifle,lycra,,print"

Comment on Re: regex capturing problem
Download Code
Re: Re: regex capturing problem
by pike (Monk) on Mar 21, 2002 at 14:12 UTC
    Doesn't really explain why I get only the last two parts as my regex started whith '^' and therefore can't just start matching in the middle of the string...

    But the correct answer has already been given (thanks, erikharrison). I just wasn't aware that ()+ gives you just one item, no matter how often it matched.


      The point is not whether it can start matching in the middle of the string but whether it can start _capturing_ in the middle of the string. By adding a + to the first part you allow the RE engine to effectively ignore the ^ anchor while still forcing it to make a match at the end of the string.
      perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
      Actually, it matches all of them, but since it is repeating, it overwrites the first match:
      $ perl -lwe"q<d'aujourd'hui> =~ /^((\w+')(?{print$+}))+([\w-]+)$/" d' aujourd' $

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-03-28 19:48 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (626 votes), past polls