Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: [Try-out] Regexp do's and don'ts

by Dietz (Curate)
on Aug 15, 2004 at 09:56 UTC ( #383071=note: print w/ replies, xml ) Need Help??


in reply to Regexp do's and don'ts

5. Do know what your regex really means.<br> Do know about ^, $, variable interpolation, the matching rules as de +scribed by the Camel Book, modifiers, and the meaning of \n (newline) + in combination with ., ^ and $
Do know about precedence since disregarding it is one of the highest crime in regex country:

I've made a sample common mistake not paying attention to precedence and I use this as a chance to pillory myself giving a perfect example of what not to do.

In node Re: Short or Long Hand I was using an alternation being based on anchors:

/^0|6$/
This simply says match 0 at the beginning or match 6 at the end while my intention was to match 0 or 6 ranging from beginning to the end of the string:
/^(?:0|6)$/

As an addition to your tutorial I'd like to see the basic requirement for regexes:
You can't write an efficient regex as long as you don't know what your expected data will be:
Always think of the expected data while changing or simplifying regexes.

I personally like the term 'regexpected'
Keep it in mind and it will save your life ;-)

Please feel free to downvote node Re: Short or Long Hand
/me castigating myself for not paying attention to precedence


Comment on Re: [Try-out] Regexp do's and don'ts
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (16)
As of 2015-07-06 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (78 votes), past polls