Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2015-11-30 14:30 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (771 votes), past polls