[% setvar title Extend regex syntax to provide for return of a hash of matched subpatterns %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


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


  Maintainer: Kevin Walker <kwalker@xmission.com>
  Date: 23 Aug 2000
  Last Modified: 30 Sep 2000
  Mailing List: perl6-language-regex@perl.org
  Number: 150
  Version: 2
  Status: Frozen


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.


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 abajace63@yahoo.com'
       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});


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.


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.


See also: RFC 112: Assignment within a regex