This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Extend regex syntax to provide for return of a hash of matched subpatterns

=head1 VERSION

  Maintainer: Kevin Walker <[EMAIL PROTECTED]>
  Date: 23 Aug 2000
  Last Modified: 30 Sep 2000
  Mailing List: [EMAIL PROTECTED]
  Number: 150
  Version: 2
  Status: Frozen

=head1 ABSTRACT

Currently regexes return matched subpatterns as a list.  This is
inconvenient in at least two situations: (1) long, complicated regexes,
where counting parentheses can be difficult and error-prone; and, more
importantly, (2) matching against a list of regexes, when the corresponding
fields of the various regexes do not occur in the same order.


=head1 DESCRIPTION

I suggest that (?% field_name : pattern) spit out 'field_name', in addition
to the matched pattern, when matching in a list context:

     $text = "abajace -- mailbox full";
        %hash = $text =~ /^ (?% username : \S+) \s*--\s* (?% reason : .*)$/xsi;

would result in %hash = (username => 'abajace', reason => 'mailbox full').

Suggestions for better syntax are hereby solicited.  (?% field_name ->
pattern) and (?% field_name => pattern) come immediately to mind.


Why This Would be Useful:

Often one wants to match a string against a list of patterns which extract
similar information from the string, but the fields occur in varying orders.
Also, some optional fields might get extracted by some patterns and not by
others.  Continuing with the (over-simplified) example of analyzing e-mail
bounce messages:

   my @regexps = (

       # 'abajace -- mailbox full' or 'abajace -- user unknown'
       q/^ \s* (?% username  : \S+) \s*--\s* (?% reason : .*)$/,
 
       # 'Unknown local part: flycrake'
       q/^ \s* (?% reason : Unknown\ local\ part): \s* (?% username  : \S+)/,
 
       # 'New address for abajace is [EMAIL PROTECTED]'
       q/(?% reason : new\ address\ for) \s+ (?% username  : \S+) \s+ is \s+
                (?% new_address : \S+\@\S+)/,

   );

   while (my $bounce_text = get_next_message()) {
       my %field = ();
       for my $regexp (@regexps) {
           if ( %field = $bounce_text =~ /$regexp/xsi;) {
               print "username: $field{username}, reason: $field{reason}\n";
               if ($field{new_address}) {
                   change_address($field{username}, $field{new_address});
               }
               last;
           }
       }
   }


Backrefs

It would also be useful to have named backrefs.  I propose that (\%field_name)
match a previous a previous named bracket.  As before, I'm not attached to
the proposed syntax.


=head1 IMPLEMENTATION

I confess that I'm not an expert in regex internals.  Nevertheless, I'll go
out on a limb and assert that this will be relatively easy to implement,
with relatively few entangling side-issues.


=head1 REFERENCES

See also: RFC 112: Assignment within a regex

Reply via email to