<?xml version="1.0" encoding="windows-1252"?>
<node id="383071" title="Re: [Try-out] Regexp do's and don'ts" created="2004-08-15 05:56:34" updated="2005-08-12 11:09:12">
<type id="11">
note</type>
<author id="366581">
Dietz</author>
<data>
<field name="doctext">
&lt;i&gt;&lt;code&gt;5. Do know what your regex really means.&lt;br&gt;
• Do know about ^, $, variable interpolation, the matching rules as described by the Camel Book, modifiers, and the meaning of \n (newline) in combination with ., ^ and $&lt;/code&gt;&lt;/i&gt;

Do know about precedence since disregarding it is one of the highest crime in regex country:&lt;br&gt;&lt;br&gt;

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.&lt;br&gt;&lt;br&gt;

In node [id://382295] I was using an alternation being based on anchors:&lt;br&gt;&lt;br&gt;
&lt;code&gt;/^0|6$/&lt;/code&gt;&lt;br&gt;
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:&lt;br&gt;
&lt;code&gt;/^(?:0|6)$/&lt;/code&gt;&lt;br&gt;&lt;br&gt;

As an addition to your tutorial I'd like to see the basic requirement for regexes:&lt;br&gt;
• You can't write an efficient regex as long as you don't know what your expected data will be:&lt;br&gt;
Always think of the expected data while changing or simplifying regexes.&lt;br&gt;&lt;br&gt;

I personally like the term 'regexpected'&lt;br&gt;
Keep it in mind and it will save your life ;-)&lt;br&gt;&lt;br&gt;


Please feel free to downvote node [id://382295]&lt;br&gt;

[Dietz|/me] castigating myself for not paying attention to precedence
</field>
<field name="root_node">
383015</field>
<field name="parent_node">
383015</field>
</data>
</node>
