Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: look-behind regex

by flocto (Pilgrim)
on Jul 24, 2002 at 10:22 UTC ( [id://184753]=note: print w/replies, xml ) Need Help??


in reply to look-behind regex

With your matching regex, you match foo and bar in a rather unelegant way. This is only needed, if you have to assure that toto and foo and in this order. This is unneccessary if

  • that doesn't matter (in this case you could call it a bug, rather than a feature..)
  • you can say: "If there are toto and foo in one line, thay are always in this order!"

In either one of this two cases, you can just use the following, which should be a lot faster (it propably doesn't make a difference with such short strings, but imagine the backtracking you get when trying to have this regex match a 1000 character long string..):

while (<DATA>) { unless (m/toto/) { s/foo/bar/g; } print; }

Once again: If you have only short strings like in your example you can happily live with your solution, if you have to ensure this order you propably have to. It's just unneccessary to match something that you don't need..

Doing all this in just one regex may be possible, but that should rather be used to write Obfuscated Code.

Regards,
-octo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-19 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found