[wellylug] Regex, replace stuff in command line help...

Grant McLean grant at mclean.net.nz
Fri Jun 30 11:25:34 NZST 2006


On Fri, 2006-06-30 at 09:27 +1200, David Antliff wrote:
> 
> On Fri, 30 Jun 2006, Grant McLean wrote:
> > If you specify the 'g' (for global matches) option after the trailing
> > slash, then the captured bits from the match are returned as an array,
> > which you can then iterate through with foreach:
> >
> > perl -nle 'print "[$_]" foreach (/profile\?u=(.*?)"/g)' filename.html
> 
> Ah, that's cool. I figured the 'g' option would do something but I wasn't 
> aware of how to get-at the results.
> 
> I wonder if Ruby does this in a similar manner?

I'm not aware of a way to get all the matches into an array in Ruby, but
the String#scan method will iterate over all matches - which is all we
were going to do with the array anyway:

ruby -ne '$_.scan(/profile\?u=(.*?)"/) { |m| puts m }' filename.html

Cheers
Grant




More information about the wellylug mailing list