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

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

Replies are listed 'Best First'.
Re: Re^5: Capturing everything after an optional character in a regex?
by davido (Cardinal) on Dec 05, 2003 at 16:44 UTC
    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

      Yes, in Perl 6, /x is on by default. It also has better support for named captures, so the example above can be expressed fine without the comments showing which numbered variable goes with which capture.

      ----
      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