From: John-John Tedro Date: 2011-08-23T00:03:01+09:00 Subject: Re: Regex find everything between --0016e6d27c9cf3be7004ab195f51 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Aug 22, 2011 at 4:53 PM, Keith Raymond wrote: > So here's the problem: > > I have a html document that is being spit out to me as a string. > > example: "\n\n \n\n > \n \t
\n \t
\n \t

My > page Testing

\n

some text here

> \t
\n \t

This is my footer > info

\n \t
\n \n" > > I'm using regular expression to find all the opening tags of the dom > elements. , , ,

, > etc... and it's working. This is via scan() method. > > ============================== > elements = [] > opening_tags = file.scan(/<\w+\s+[^>]*>/) > opening_tags.each do |tag| > if tag.match(/class=\\"(.*?)editor(.*?)\\"/) # tries to match anything > with a class="editor" > close = get_closing_tag(tag) > # finds which DOM element it is and returns close tag > # example if '

' returns '

' > file.match(/#{tag}(.+)#{close}]/) { |m| elements << m } > # pushes all matches to elements array > > ======================================= > > So I get the opening tags as it should >

and

> and I get a proper closing tag for each >

and

> but /#{tag}(.+)#{close}]/ returns nothing > > Output from Rails.logger.info > +++++++++++++++++++++++++++++++++++++++ > ==== tag ==== > "

" > ==== close ==== > "

" > ==== /#{tag}(.+)#{close}]/ ==== > /

(.+)<\/p>]/ > ==== tag ==== > "

" > ==== close ==== > "

" > ==== /#{tag}(.+)#{close}]/ ==== > /

(.+)<\/p>]/ > ==== tag ==== > "

" > ==== close ==== > "

" > ==== /#{tag}(.+)#{close}]/ ==== > /

(.+)<\/p>]/ > ======= elements ======== > [] > > +++++++++++++++++++++++++++++++++++++++ > > Any help would be appreciated. I'm at my wits end here. If there is a > completely better way to do this, I'm all ears as well. > > Thank you in advance. > > -- > Posted via http://www.ruby-forum.com/. > > Try out nokogiri: https://github.com/tenderlove/nokogiri After you've let it parse your document you can use css3 or xpath selectors to find what you are looking for. Letting someone else do all the dirty work is a good idea for potentially dirty html. -- John-John Tedro --0016e6d27c9cf3be7004ab195f51--