in reply to Re^2: RFC: named pattern match tokens
in thread RFC: named pattern match tokens
I thought the same. I think a nice name of the hash would be %~. =~ is matching so why couldn't $~{name} be a named match. Here is the code I ended up with:
Please note that even the named matches got their number! Maybe they should not, I think I could implement that if I needed.... sub convert { my $re = shift; $re =~ s( \\ ( \\ | C\{ (?>\s*) ((?>\w+)) (?>\s*) \} ) ) { defined $2 ? "(?{\$~{$2}=\$^N})" : "\\" }xeg; "(?{undef(%~)})" # clear the %~ .$re ."(?{\$~{\$_}=\${\$_} for(1..\$#+)})"; # add the numbered matches } ... my $re = qr/(\w+)\C{baz}(?: - (\w+)\C{qux})?(\+\d+)/; "three - four - five+89" =~ $re; print "baz=$~{baz}, qux=$~{qux}, $~{3}\n";
I also considered syntax like this:
which could naively be implemented like this:my $re = qr/(?\$bar=\w+) - (?\$qux{not}=\w+)/;
but the problem is that I don't know how to make sure you can do even things like :... sub convert { my $re = shift; $re =~ s<\(\?\\\$([^=]+)=([^)]*)\)><($2)(?{\$$1=\$^N})>g; $re } ...
I don't know how to find the right closing bracket.my $re = qr/...(?\$var=a(\d+|\w-\w+)b).../;
Jenda
We'd like to help you learn to help yourself Look around you, all you see are sympathetic eyes Stroll around the grounds until you feel at home -- P. Simon in Mrs. Robinson |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: RFC: named pattern match tokens
by diotalevi (Canon) on Oct 04, 2004 at 23:10 UTC |