Front page | perl.perl6.announce.rfc |
Postings from September 2000
RFC 360 (v1) Allow multiply matched groups in regexes to return a listref of all matches
From:
Perl6 RFC Librarian
Date:
September 30, 2000 23:43
Subject:
RFC 360 (v1) Allow multiply matched groups in regexes to return a listref of all matches
Message ID:
20001001063813.12169.qmail@tmtowtdi.perl.org
This and other RFCs are available on the web at
http://dev.perl.org/rfc/
=head1 TITLE
Allow multiply matched groups in regexes to return a listref of all matches
=head1 VERSION
Maintainer: Kevin Walker <kwalker@xmission.com>
Date: 30 Sep 2000
Mailing List: perl6-language-regex@perl.org
Number: 360
Version: 1
Status: Developing
=head1 DESCRIPTION
Since the October 1 RFC deadline is nigh, this will be pretty informal.
Suppose you want to parse text with looks like:
name: John Abajace
children: Tom, Dick, Harry
favorite colors: red, green, blue
name: I. J. Reilly
children: Jane, Gertrude
favorite colors: black, white
...
Currently, this takes two passes:
while ($text =~ /name:\s*(.*?)\n\s*
children:\s*(.*?)\n\s*
favorite\ colors:\s*(.*?)\n/sigx) {
# now second pass for $2 ( = "Tom, Dick, Harry") and $3, yielding
# list of children and favorite colors
}
If we introduce a new construction, (?@ ... ), which means "spit out a
list ref of all matches, not just the last match", then this could be
done in one pass:
while ($text =~ /name:\s*(.*?)\n\s*
children:\s*(?:(?@\S+)[, ]*)*\n\s*
favorite\ colors:\s*(?:(?@\S+)[, ]*)*\n/sigx) {
# now we have:
# $1 = "John Abajace";
# $2 = ["Tom", "Dick", "Harry"]
# $3 = ["red", "green", "blue"]
}
Although the above example is contrived, I have very often felt the need
for this feature in real-world projects.
=head1 IMPLEMENTATION
Unknown.
=head1 REFERENCES
None.
-
RFC 360 (v1) Allow multiply matched groups in regexes to return a listref of all matches
by Perl6 RFC Librarian