From: William James Date: 2008-02-29T00:55:25+09:00 Subject: Re: Need a regex searching html code On Feb 28, 12:36 am, Chirantan wrote: > I have an html code into string. I want to retrieve the content (Can > be any HTML code with any number of tags) present inside the div after > the heading till the end of the div. > > Example, > >
>
Tagline:
> Yippee Ki Yay Mo - John 6:27 >
> >
>
Plot Outline:
> John McClane takes on an Internet-based terrorist organization who is > systematically shutting down the United States. onclick="(new Image()).src='/rg/title-tease/plotsummary/images/b.gif? > link=/title/tt0337978/plotsummary';">more >
> > In the above example, Plot Outline is header that I am looking for > then, regex should give me - > > John McClane takes on an Internet-based terrorist organization who is > systematically shutting down the United States. onclick="(new Image()).src='/rg/title-tease/plotsummary/images/b.gif? > link=/title/tt0337978/plotsummary';">more > > And if "Tagline:" is what I am looking for then regex should give me - > > Yippee Ki Yay Mo - John 6:27 > > I hope the problem statement is clear. Note that this will give spurious results if an html comment happens to contain what you are looking for. def find_header header, html # Put all of the DIVs in an array. divs = html.scan( %r{(.*?)}im ).flatten divs.each{|s| if s =~ %r{#{header}(.*)}im return $2.strip end } return nil end html = DATA.read puts find_header( "Plot Outline:", html ) __END__
Tagline:
Yippee Ki Yay Mo - John 6:27
Plot Outline:
John McClane takes on an Internet-based terrorist organization who is systematically shutting down the United States. more