From: Logan Capaldo Date: 2006-07-15T03:59:43+09:00 Subject: Re: Help with a regexp On Jul 14, 2006, at 2:16 PM, dblack@wobblini.net wrote: > Hi -- > > On Thu, 13 Jul 2006, Daniel Martin wrote: > >> Daniel Schierbeck writes: >> >>> I'm trying to write a regular expression that matches bencoded >>> strings, i.e. strings on the form x:y, where x is the numeric length >>> of y. >>> >>> This is valid: >>> >>> 6:foobar >>> >>> while this is not: >>> >>> 4:foo >> >> I don't think that what you want to do is possible with a mere >> regular >> expression. >> >> It might be possible using perl's special >> evaluate-code-while-in-regexp (??{ code }) feature, but not with any >> language that doesn't allow regular expression evaluations to escape >> back into the host language. > > Is ??{ code } in Perl different from #{...} in Ruby? (Not that I was > able to solve Daniel's problem with #{...}, but I'm just curious about > the comparison.) > According to _Programming Perl_ yes indeedy it is. ??{ } is "Match Time Pattern Interpolation", and it lets you do all sorts of evil (like matching nested parens with a regexp). So in perl his code would be something like: % cat mtpi.pl $s1 = "3:abc"; $s2 = "24:abc"; print "Good\n" if ( $s1 =~ /(\d+):(??{'\w{' . $1 . '}'})/); print "Bad\n" if ( $s2 =~ /(\d+):(??{'\w{' . $1 . '}'})/); % perl mtpi.pl Good I apologize to any perlers if this isn't idiomatic (or clean) perl, I never had to use this kind of magic in my perl days and I had difficulty getting it to work when I stored the regexp in a variable. But the point is, is that it does work. Which is kind of scary. > > David > > -- > http://www.rubypowerandlight.com => Ruby/Rails training & consultancy > http://www.manning.com/black => RUBY FOR RAILS (reviewed on > Slashdot, 7/12/2006!) > http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log > dblack@wobblini.net => me >