in reply to Re: Re: Capturing everything after an optional character in a regex?
in thread Capturing everything after an optional character in a regex?

Of course there is the issue now of counting capturing parens.

That's what the /x modifier is there to help for.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: Re: Capturing everything after an optional character in a regex?
by davido (Cardinal) on Dec 04, 2003 at 23:09 UTC
    You had me for a second. I even went back and checked perlre to see how on earth the /x modifier would eliminate the fact that my RE will load $1 under one alternation option, and $2 under the other alternation option. Of course there's nothing there other than what I expected to find: that the /x modifier allows for whitespace and comments within regexes.

    Are you suggesting that with /x it is somehow easier to determine which set of capturing parens loaded up the $1 and $2 special variables? Regular expression readability notwithstanding, I'm missing the point I guess.

    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 the point is moot I guess, since sauoq's answer seems to have solved the OP's problem in a more graceful way anyway.


    Dave

      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.";
      

      Just to give an example of how /x can help here, look at the code below. Let us assume that $_ contains data in the form id|name|address|city|state|postal. So:

      /\A ( # $1 -- Entire string (hey, why not?) ([^|]+) \| # $2 -- id ([^|]+) \| # $3 -- address ([^|]+) \| # $4 -- city ([^|]+) \| # $5 -- state ([^|]+) # $6 -- postal )\z/x;

      IMHO, it's more impressive to make smart use of \x than being able to make use of bizzare regex features.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        I remember reading in Exegisis (or whatever the doc is called) that the Perl 6 regex behavior will, among other things, default to /x type behavior.

        Dave