develooper Front page | perl.perl6.language.regex | Postings from August 2000

RFC 112 (v2) Assignment within a regex

Thread Next
From:
Perl6 RFC Librarian
Date:
August 27, 2000 12:01
Subject:
RFC 112 (v2) Assignment within a regex
Message ID:
20000827190145.19037.qmail@tmtowtdi.perl.org
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1	TITLE

Assignment within a regex

=head1 VERSION

	Maintainer: Richard Proctor <richard@waveney.org>
	Date: 16 Aug 2000
	Date: 27 Aug 2000
	Version: 2
	Mailing List: perl6-language-regex@perl.org
	Number: 112

=head1 ABSTRACT

Provide a simple way of naming and picking out information from a regex
without having to count the brackets.

=head1 CHANGES

Version 2 of this RFC redirects discussion of this topic to
perl6-language-regex@perl.org.

=head1 DESCRIPTION

If a regex is complex, counting the bracketed sub-expressions to find the
ones you wish to pick out can be messy.  It is also prone to maintainability
problems if and when you wish to add to the expression.  Using (?:) can be
used to surpress picking up brackets, it helps, but it still gets "complex".  
I would sometimes rather just pickout the bits I want within the regex itself.

Suggested syntax: (?$foo= ... ) would assign the string that is matched by
the patten ... to $foo when the patten matches.  These assignments would be
made left to right after the match has succeded but before processing a 
replacement or other results.  There may be whitespace between the $foo and
the "=".  This would not give the backrefs \1 etc that come with conventional
bracketed sub expressions, I don't think this would be a problem.
Potentially the $foo could be any scalar LHS, as in (?$foo{$bar}= ... )!,
likewise the '=' could be any asignment operator.

The camel and the docs include this example:

   if (/Time: (..):(..):(..)/) {
        $hours = $1;
        $minutes = $2;
        $seconds = $3;
    }
        
This then becomes:
 
  /Time: (?$hours=..):(?$minutes=..):(?$seconds=..)/

This is more maintainable than counting the brackets and easier to understand
for a complex regex.  And one does not have to worry about the scope of $1 etc.

(Note I am only on the announce list at present as I am suffering
from negative free time).

=head1 IMPLENTATION

No idea

=head1 REFERENCES

I brought this up on p5p a couple of years ago, but it was lost in the noise...




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