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

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"

Replies are listed 'Best First'.
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-04-21 00:59 GMT
Find Nodes?
    Voting Booth?