Regular expression readability notwithstanding, I'm missing the point I guess.

I think readability was exactly his point. Use /x and comment where each capture starts. I see both of your points though I do think that hardburn went off on a bit of a tangent by bringing up /x in the first place. Using the modifier certainly doesn't make it any easier to deal with the two sets of parens in your code, just in your mind.

The easiest solution I see to the counting problem is just to wrap the entire expression in an outter set of parens so that $1 captures both the right or the left side of the alternation.

But you can't do that because then you'd be capturing the 'X' in the first alternative which is just what you want to avoid.

But the point is moot I guess, since sauoq's answer seems to have solved the OP's problem in a more graceful way anyway.

Or not. The problem being that he never really did tell us exactly what he wanted. Sigh and shrug.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Re: Re: Re: Re: Capturing everything after an optional character in a regex? by sauoq
in thread Capturing everything after an optional character in a regex? 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.