develooper Front page | perl.perl5.porters | Postings from May 2003

Re: [PATCH] Extra newline in stringified qr/\#/x

Thread Previous | Thread Next
From:
Ronald J Kimball
Date:
May 30, 2003 10:35
Subject:
Re: [PATCH] Extra newline in stringified qr/\#/x
Message ID:
20030530173100.GC67936@linguist.thayer.dartmouth.edu
On Fri, May 30, 2003 at 04:25:48PM +0100, Orton, Yves wrote:
> > As far as I can tell, the stringified form of a regexp is only useful
> > for debugging purposes, to dump the inner contents of a regexp.
> 
> On the contrary. The ability to stringify a regex is a crucial aspect power
> and flexibility of perls regexes.
> 
> Consider the following.... (conceptual demonstration):
> 
> my $iso_date_capture=qr!(\d{4})[-/\\](\d{2})[-/\\](\d{2})!;
> (my $iso_date_match=$iso_date_capture)=~s/\((?:\?[msiox-]*\:)?/(?:/g;
> $iso_date_match=qr/$iso_date_match/;
> 
> print $iso_date_capture,"\n";
> print $iso_date_match,"\n";
> 
> By being able to stringify a regex we can manipulate them and generate new
> regexes. This is not debugging this is good practice. The example above
> takes a regex that captures and produces a regex that doesnt capture
> mechanically.

I'm skeptical that modifying a stringified version of a regex in this way
to create a new regex is really good practice.  Is the exact output of the
stringified regex actually specified?

Nonetheless, if you do do this the substitution should be more robust,
because the above example deletes any flags in the regex, and will break
any regex that tries to match a literal left parenthesis.

One way to do it:

s/(\\.)|\((\?[msix-]*\:)?/$1 || ('(' . ($2 || '?:') )/ge;

Ronald

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About