Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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;
hth
   larryk                                          
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.

    pike

      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.
         larryk                                          
      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' $
          p

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-07-28 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (199 votes), past polls