Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex problem - (non)greedy?

by ikegami (Pope)
on Nov 14, 2013 at 14:18 UTC ( #1062589=note: print w/replies, xml ) Need Help??


in reply to Regex problem - (non)greedy?

Why do matches aginst (.*) and (.*?) result in exactly the same result?

Because there's only one ghi.

... /c/ matches "c" (at position 3) /d/ matches "d" /.*/ matches " cd EF ghi jkl" /g/ fails -> backtrack /.*/ matches " cd EF ghi jk" /g/ fails -> backtrack /.*/ matches " cd EF ghi j" /g/ fails -> backtrack ... /.*/ matches " cd EF " /g/ matches "g" /h/ matches "h" /i/ matches "i" -> success
... /c/ matches "c" (at position 3) /d/ matches "d" /.*?/ matches "" /g/ fails -> backtrack /.*?/ matches " " /g/ fails -> backtrack /.*?/ matches " c" /g/ fails -> backtrack ... /.*?/ matches " cd EF " /g/ matches "g" /h/ matches "h" /i/ matches "i" -> success

What needs to be done to get only the "EF" as a result?

(?s:(?!STRING).)* is to STRING as [^CHAR]* is to CHAR.

/cd(?:(?!cd|ghi).)ghi/s

Note: Unlike the trick of adding ^.* to the start of the pattern, this pattern can be used in other patterns.

Note: I think want "EF" instead of " EF ", but it's easier just to trim the whitespaces afterwards.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1062589]
help
Chatterbox?
[marto]: Eily, there's actually a lot of good discussion on the topic in the HN link choroba posted
[marto]: sorry for the late response, busy busy :/
[Corion]: I think you need to load Readline in the debugger, but I don't know how
[Corion]: Yay, invitation for a team-building workshop, with a team of 100 people :-/
[Eily]: marto after an additional read I started to see the light actually. And choroba's link was helpful as well :)
[Corion]: I'll send my colleague who doesn't yet know the people there, and I'll do production duty instead.
[GotToBTru]: ugh. Thanks Corion

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2017-07-26 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (396 votes). Check out past polls.