From: Aaron Reimann Date: 2006-08-02T23:15:11+09:00 Subject: Re: super-newbee Ruby regex help? Thank you guys. I have not tried all that has been suggested, but I got this code emailed to me: ### require 'rubygems' require 'mechanize' url="http://edge.i-hacked.com/250-working-proxies-for-safe-web-access-from-work-or-school" agent = WWW::Mechanize.new page = agent.get(url) page.body.scan(/http:\/\/www\.([^"]+)/) do p $1 end ### I had to install the 'mechanize' gem, but it works...overall. I have to figure out how to "write" the output into a text file. but this is pretty cool. I will be trying the one below too. thanks! aaron Vincent Fourmond wrote: > Hello ! > > > In that page there is one line of "code" that has all of the > > links...here is part of it: > > 3 Proxy || > href="http://www.3proxy.net">3 Proxy || > href="http://www.3proxy.org">3 Proxy > > > > I have taken just that line and saved that as a text file. > > > > I need to strip everything where I wind up with this: > > 3proxy\.com > > 3proxy\.net > > 3proxy\.org > > 4proxy\.com > > > > OK, what you need is to extract the part 3proxy.com from the String > 3 Proxy > > For that, a RE like the following should do > > /http:\/\/www\.([^"]+)/ > > You can read it this way: "find substrings that start with http://www. > (don't forget to escape /in the RE, else ruby will think that it is > ending; you also need to escape the dot, although in this case it > shouldn't matter much) > and are followed by some text that doesn't contain ". The parenthesis > around say you're interested in it; you'll be able to use what it did > match with the $1 variable. Note that this part will match as much as > possible, so you'll actually get everything you want. > > Then a possible way to do what you want would be > > proxies = [] # array where the proxies will be > f = File.open('your_file_with_the_list_youre_reading') > f.readlines.each do |l| # iterate on each line > l.scan(/http:\/\/www\.([^"]+)/) do # scan the line for the pattern > proxies << $1 # add the content of $1 to your list > end > end > p proxies > > This should work... > > Have a good time with Ruby ! > > Vince