Could I specify pattern match priority in lex code?
<p>I've got a related thread in the site(<a href="https://stackoverflow.com/questions/39606840/my-lex-pattern-doesnt-work-to-match-my-input-file-how-to-correct-it">My lex pattern doesn&#39;t work to match my input file, how to correct it?</a>)</p>
<p>The problems I met, is about how &quot;greedy&quot; lex will do pattern match, e.g. I've got my lex file:</p>
<pre><code>$ cat b.l
%{
#include&lt;stdio.h&gt;
%}
%%
&quot;12&quot; {printf(&quot;head\n&quot;);}
&quot;34&quot; {printf(&quot;tail\n&quot;);}
.* {printf(&quot;content\n&quot;);}
%%
</code></pre>
<p>What I wish to say is, when meet &quot;12&quot;, print &quot;head&quot;; when meet &quot;34&quot;, print &quot;tail&quot;, otherwise print &quot;content&quot; for the longest match that doesn't contain either &quot;12&quot; or &quot;34&quot;.</p>
<p>But the fact was, &quot;.*&quot; was a greedy match that whatever I input, it prints &quot;content&quot;.</p>
<p>My requirement is, when I use</p>
<blockquote>
<p>12sdf2dfsd3sd34</p>
</blockquote>
<p>as input, the output should be</p>
<pre><code>head
content
tail
</code></pre>
<p>So seems there're 2 possible ways:</p>
<blockquote>
<p>1, To specify a match priority for &quot;.*&quot;, it should work only when neither &quot;12&quot; and &quot;34&quot; works to match. Does lex support &quot;priority&quot;?</p>
<p>2, to change the 3rd expression, as to match any contiguous string that doesn't contain sub-string of &quot;12&quot;, or &quot;34&quot;. But how to write this regular expression?</p>
</blockquote>