<?xml version="1.0" encoding="windows-1252"?>
<node id="407092" title="Re^3: Why machine-generated solutions will never cease to amaze me" created="2004-11-11 11:18:26" updated="2005-06-21 04:13:46">
<type id="11">
note</type>
<author id="109406">
tmoertel</author>
<data>
<field name="doctext">
[id://209409] wrote:

&lt;blockquote&gt;&lt;em&gt;Regexp union is not commutative when one of the
alternates is a leading substring of another: then order becomes
important - (E|x) will always match E in preference to x.&lt;/em&gt;&lt;/blockquote&gt;

Excellent point.  (However, union is commutative even in the face of overlapping operands when the union is forced to match all of the target string or not match at all.)

&lt;blockquote&gt;&lt;em&gt;It is the presence of the outer anchors in the original pattern
that disambiguates and thus makes (regexp union)
commutative.&lt;/em&gt;&lt;/blockquote&gt;

Even anchoring isn't sufficient.  What if one of the anchors matches
the difference between the union operands?  Consider
&lt;code&gt;(a|aa)&lt;/code&gt; in:

&lt;code&gt;
  "aaaa" =~ /a(a|aa)a/
&lt;/code&gt;

&lt;p&gt;Fortunately, in the case of the OP's regexes &amp;#8211;
&lt;code&gt;s(?:al|pre)?ad&lt;/code&gt; and &lt;code&gt;s(?:(?:al)?|pre)ad&lt;/code&gt;)
&amp;#8211; the union operands don't overlap, and so classical equivalence
(do the regexps generate the same language?)  predicts the equivalence of
Perl's matching behavior (do the regexps match the same strings
identically?)

&lt;p&gt;Cheers,&lt;br&gt;Tom

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-109406"&gt;
&lt;p&gt;&lt;small&gt;&lt;strong&gt;Tom Moertel&lt;/strong&gt; : &lt;a href="http://community.moertel.com/"&gt;Blog&lt;/a&gt; / &lt;a href="http://community.moertel.com/ss/space/Talks"&gt;Talks&lt;/a&gt; / &lt;a href="http://search.cpan.org/~tmoertel/"&gt;CPAN&lt;/a&gt; / &lt;a href="http://community.moertel.com/LectroTest"&gt;LectroTest&lt;/a&gt; / &lt;a href="http://community.moertel.com/ss/space/PXSL"&gt;PXSL&lt;/a&gt; / &lt;a href="http://community.moertel.com/ss/space/A+Coder%27s+Guide+To+Coffee"&gt;Coffee&lt;/a&gt; / &lt;a href="http://community.moertel.com/ss/space/IMDB+Movie-Rating+Decoder+Ring"&gt;Movie Rating Decoder&lt;/a&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;
</field>
<field name="root_node">
406643</field>
<field name="parent_node">
407031</field>
</data>
</node>
