Thursday, 15 July 2010

ruby - What is the best way to match id's against a regular expression in Hpricot? -


Looks beautiful, it is very easy to see how I can remove all elements with a given ID or class using the CSS selector. Is it possible to remove elements from a document based on a match against some regular expressions of some of those elements?

If you want to do something like this:

  doc.search ("// div [@ id = / regex /]")  

So I do not think it can be done. Alternatives are to find all the elements and then remove the results that do not match a regex.

  result = doc.search ("// div") results .delete_if (| X | x.to_s! ~ / Regex /)  

Many The alternative approach is I. There are two other suggestions in this thread.

Note, depending on the fact that you are trying to match, you may be able to use the "supported, but different" syntax, for example:

  e [@foo $ = "bar"]  


No comments:

Post a Comment