From: Jari Williamsson Date: 2008-03-01T04:36:37+09:00 Subject: Re: Need a regex searching html code Todd Benson wrote: > On Fri, Feb 29, 2008 at 1:19 PM, Jari Williamsson > wrote: >> Mark Thomas wrote: >> > All the regex solutions provided will break with the following >> > perfectly valid HTML: >> > >> >
>> >
Tagline:
>> > Yippee Ki Yay Mo - John 6:27 >> >
>> > >> > This is one of many reasons it is a BAD idea to use regexes to parse >> > HTML. Regular expressions are simply not the right tool for the job. >> >> Sorry if I'm missing the point: >> --- >> the_text = %q{ >> >>
>>
Tagline:
>> Yippee Ki Yay Mo - John 6:27 >>
>> } >> >> the_text.each_line do |line| >> puts "Within DIV tags: #{line}" if (line=~/
> puts "Within H5 tags: #{line}" if (line=~/
> end >> --- >> >> Result: >> Within DIV tags:
>> Within DIV tags:
Tagline:
>> Within H5 tags:
Tagline:
>> Within DIV tags: Yippee Ki Yay Mo - John 6:27 >> Within DIV tags:
> > What if you have a div inside a div? Although, the OP said "any" > legitimate html inside a div, there's part of me that begs the > question: which div? Sure, for real-life HTML with nested tags it'll break. I just wanted to point out that for simple parsing needs (as the example that I replied to) regexps can find both beginning and end tags. Best regards, Jari Williamsson