2

I'm trying to complete an assignment where I am supposed to write a Ruby regular expression to capture items between html tags but I'm really stuck. I've searched everywhere but I can only find advice about using html parsers and other programs that I don't think we are allowed to use because we have only learned regular expressions so far.

The example text is:

<span id="animal_display">
    <a href="/b/bird">Bird</a>     
    <a href="/c/cat">Cat</a>
    <a href="/c/dog">Dog</a>
</span>

I'm trying to capture Bird Cat Dog

Using this regular expression, I am able to get the first occurrence:

 /<span id="animal_display">.*?<[^>]+>(.*?)<\/[^>]+>.*<\/span>/m

I can get all three with this, but I want to be able to use the regular expression on lists that might have more than three items:

 /<span id="animal_display">\s*<[^>]+>\s*(.*?)<\/a>.\s*<[^>]+>\s*(.*?)<\/a>.\s*<[^>]+>\s*(.*?)<\/a>.<\/span>/

Is there a more generalized regular expression that could work on an unspecified number of items? Any advice would be greatly appreciated.

1
  • Don't use regexes to parse HTML. Your life will be sorrow, and there are already existing tools that will do it for you. Nov 27, 2012 at 4:14

4 Answers 4

1

This isn't a complete answer, but sometimes a hairy capturing regex can be simplified by tackling the problem from the other direction -- using split:

html  = '...'
r     = / <a[^>]*>\s* | <\/a>[^<]+ /mx
parts = html.split(r)

parts.each { |p| puts p.inspect }

# Output
"<span id=\"animal_display\">                "
"Bird"
""
"Cat"
""
"Dog"
"</span>"
1
  • Thanks so much! I'll upvote this as soon as I get enough points on this thing.
    – SophiaAP
    Nov 27, 2012 at 5:01
1

I think it would make your life easier with a convenient dom parser.

https://www.ruby-toolbox.com/categories/html_parsing.html

I'd recommend checking out the Ruby Toolbox site.

You'll notice that Nokogiri is the top recommendation for HTML parsers but you should check out hpricot. It's exceedingly good. It's not 'core' ruby, but it's a commonly used gem.

0

try this one

str.gsub(/</?[^>]*>/,"")

0

In real life I would use Nokogiri to parse this, however the course you are on seems determined to teach you the incorrect way to do things, so here is a way to get the result without using a parser:

Firstly:

x = '<span id="animal_display">
         <a href="/b/bird">Bird</a>     
         <a href="/c/cat">Cat</a>
         <a href="/c/dog">Dog</a>
     </span>'

Then:

x.scan(/<a.*?>(.*?)<\/a>/).flatten
 => ["Bird", "Cat", "Dog"]

I am not sure if there is a way to create a regex that will match and return the right results for an arbitrary number of list items; it may be possible using subexpression calls but that's very complex.

1
  • Thanks so much! I'll upvote this as soon as I get enough points on this thing.
    – SophiaAP
    Nov 27, 2012 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.