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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: look-behind regex by flocto
in thread look-behind regex by zejames

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found