I think you might find it worthwhile to learn about /x at this point as these regular expressions could certainly do with some commenting. /x isn't hard or scary at all. All you have to do is rememeber to escape the whitespace you want and the #s. It makes regular expressions much easier to explain.

Just to make things more confusing ;) I'm going to swap the order of these two expressions, so my first one will be the longer of the two and I'll work on the the shorter (my second - your first) as I think that's the one you wanted to focus on.

To determine if your two regular expressions are suffiently equivalent we need to compare them.

This is the longer one:

/.* # Stuff ( # START capturing to $1 [$\ \#\%>~] # Any single space, $, #, %, > or ~ | # OR \[* # 0 or more [s \w* # 0 or more word characters (a-zA-Z0-9_) \@* # 0 or more @s \-* # 0 or more -s \w* # more word characters \% # Exactly 1 % \]* # 0 or more ]s | # OR \[*\w*\@*\-*\w*\#\]* # As above, but with a # instead of % | # OR \[*\w*\@*\-*\w*\$\]* # As above, but with a $ | # OR \[*\w*\@*\-­*\w*>\]* # As above, but with no terminator # (will therefore match any terminator) | # OR \\\[\\e\[0m\\\]\ \[0m # the sequence: \[\e[0m\\] [0m ) # END of $1 \s? # 0 or 1 spaces /x
and this the shorter:
/.* # Stuff ( # START capturing to $1 \[* # 0 or more [s \w* # 0 or more word characters \@* # 0 or more @s \-* # 0 or more -s \w* # more word characters [$\ \#\%>~] # exactly 1 space, $, #, %, > or ~ \] # exactly 1 ] (are you missing a * ?) | # OR \\\[\\e\[0m\\\]\ \[0m # the sequence: \[\e[0m\\] [0m ) # END of $1 \s? # 0 or 1 spaces /x
Now we need to consider what patterns will match one, but not the other... (I'm going to assume you are missing a * up there next to your ], if not, then these aren't very equivalent at all).

Like you, I can only spot this one significant difference between the two regular expressions (once you fix your typo).

This is easily fixed:
/.* # Stuff ( # START capturing to $1 \ # exactly 1 space | # OR \[* # 0 or more [s \w* # 0 or more word characters \@* # 0 or more @s \-* # 0 or more -s \w* # more word characters [$\#\%>~] # exactly 1 of $, #, %, > or ~ \]* # 0 or more ]s | # OR \\\[\\e\[0m\\\]\ \[0m # the sequence: \[\e[0m\\] [0m ) # END of $1 \s? # 0 or 1 spaces /x
Note that this equivalence won't necessarily remain true if you change your quantifiers. In particular if you change all of your *s to ?s. If you want my opinion I suspect you're actually looking more for a regular expression like this:
/.* # Stuff ( # START capturing to $1 \ # exactly 1 space | # OR \[? # 0 or 1 [ \w* # 0 or more word characters \@? # 0 or 1 @ [-\w.]* # 0 or more word chars, dots and hyphens eg +w-w.w-.w [$\#\%>~] # exactly 1 of $, #, %, > or ~ \]? # 0 or 1 ] | # OR \\\[\\e\[0m\\\]\ \[0m # the sequence: \[\e[0m\\] [0m ) # END of $1 \s? # 0 or 1 spaces /x

But I may be wrong - you may not be interested in the dot at all. ;) I'm not 100% certain that you want the .* at the front though. Do you have some sample data for us?

I hope you recognise that both expressions will match any string with a single space in it... which will be most strings....

I hope this helps.

jarich


In reply to Re: regex logical equivalence? by jarich
in thread regex logical equivalence? by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.