From: James Edward Gray II Date: 2006-03-08T05:06:21+09:00 Subject: Re: Is there link extractor or similar html processing libs for Ruby On Mar 7, 2006, at 1:36 PM, Bill Kelly wrote: > From: "James Edward Gray II" >> > >> > You meant something like this ? (quite dirty but works) >> > >> > puts open("some.html").read.scan(//) >> No, it doesn't, trust me. ;) Toss a simple "\n" in there and >> you're sunk: >> > href="whatever"> >> Parsing HTML is hard and you don't want to use regular expressions >> to do it. > > Hi, not trying to be argumentative, just surprised. I thought > parsing HTML with regexps was pretty easy. Well, lexing HTML into > tokens, I mean. There's a lot of pretty darn ugly HTML out there my friend. Here's a semi-paranoid attempt to grab just the start of anchor tag: /<\s*a[^>]*?href\s*=\s*(['"]?)[^'"]*\1?[^>]*>/i Am I getting close yet? No, the quotes are all wrong. That would fail to match an extremely common link like: I would try to fix that, but my brain has already melted and leaked out my ear. :) I'm sure I made other mistakes too. If you want to capture the name of the link too, this gets *much* worse! James Edward Gray II