From: Jeff 'japhy' Pinyan Date: 2002-02-07T07:02:57+09:00 Subject: Re: Regexp::Parser ported to Ruby...? [posted & mailed] On Feb 6, Tobias Reif said: >Jeff 'japhy' Pinyan wrote: > >> "japhy" =~ /((.)(?{ print "<$2>" }))+/; >> #

> >One way: > >irb(main):003:0> "japhy".gsub(/./)do print "<#{$&}>" end >

"" Ok, let me be a bit more specific. A common request is to be able to have NAMED captures. In Perl regexes, "japhy" =~ /(..)(..)$/ stores "ap" to $1 and stores "hy" to $2. Some people would like the ability to assign DIRECTLY to some variable, like: "japhy" =~ /(?<$front>..)(?<$back>..)$/; Well, this syntax isn't possible in Perl (yet). However, with my regex parser, it is. My regex parser will parse the non-existent (?PAT) construct and turn it into working Perl regex code. I present it in an expanded format for legibility (?): "japhy" =~ m{ (?{ local (@S, @E) }) # creates two local arrays for use in the regex only (?{ local $S[0] = $+[0] }) # our current position in the string .. (?{ local $E[0] = $+[0] }) # our current position in the string (?{ local $S[1] = $+[0] }) # our current position in the string .. (?{ local $E[1] = $+[0] }) # our current position in the string $ (?{ $front = substr($&, $S[0], $E[0] - $S[0]) }) (?{ $back = substr($&, $S[1], $E[1] - $S[1]) }) }x; The local() makes sure that when backtracking is done, values are restored as needed, etc. The end result is a regex that stores "ap" in $front, and "hy" in $back. -- Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** what does y/// stand for? why, yansliterate of course.