[% setvar title Allow multiply matched groups in regexes to return a listref of all matches %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Allow multiply matched groups in regexes to return a listref of all matches
Maintainer: Kevin Walker <kwalker@xmission.com> Date: 30 Sep 2000 Mailing List: perl6-language-regex@perl.org Number: 360 Version: 1 Status: Developing
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.
Unknown.
None.