From: Robert Klemme Date: 2004-04-14T23:14:12+09:00 Subject: Re: embedded code construct a la perl "Vince Forgetta" schrieb im Newsbeitrag news:407D3FEC.6050707@staff.mcgill.ca... > I want to find all nested pattern matches in a string, and execute some > code at each match. This should include searching all nested substrings > of a match. I realize I could do this in perl ( I have been up to now), > but my code is getting quite complex and I would like to implement an > object model (something I could also do in perl, but works much better > in Ruby). In perl, I was able to do this in a regexp (example below). > Can I do the same thing in ruby, or will I need to use some sort of loop > to wun code at each match. As said, no code in Ruby regexps. You'll have to do looping. str.scan rx do |m| doForAllSubstringsOf m do |subString| puts subString end end The funny part is implementing doForAllSubstringsOf(str). :-) Well, it isn't really difficult: def doForAllSubstringsOf(str) len = str.length for i in 0 ... len for j in 1 ... len - i yield str[i, j] end end end Regards robert > > Thanks. > > Vince > > > Robert Klemme wrote: > > >"Vince Forgetta" schrieb im Newsbeitrag > >news:407C1AD7.3040406@staff.mcgill.ca... > > > > > >>Hi all, > >> > >>Is it possible to use embedded-code constructs in ruby regexp? > >> > >>Perl Example: > >> > >> > >> > >>"123"= ~ m{ > >> > >> > > > >I guess, this should have read > >"123" =~ m{ > > > > > > > >>\d+ > >>(?{print "matched at [$'<$&>$']\n"}) > >>(?!) > >>}x; > >> > >> > >> > >>Output: > >> > >>matched at [<123>] > >>matched at [<12>3] > >>matched at [<1>23] > >>matched at [1<23>] > >>matched at [1<2>3] > >>matched at [12<3>] > >> > >> > > > >No. If you let us know what you want to do mayve we can come up with an > >alternative solution. > > > >Regards > > > > robert > > > > > > > > > > > > > > > -- > Vincenzo Forgetta > > Computational Biology > McGill University and Genome Quebec Innovation Centre > 740 Dr. Penfield Avenue Room 7211 > Montreal, Quebec Canada, H3A 1A4 > > Tel: 514-398-3311 00476 > Email: vince.forgetta@staff.mcgill.ca > > >