1

I have the following regular expression in Ruby:

\<name\>(.+)\<\/name\>

Within in if statement, like so:

if line =~ /\<name\>(.+)\<\/name\>/

Is there any way to get the value of the group (.+)?

Thanks in advance!

maček
  • 76,434
  • 37
  • 167
  • 198
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87
  • I think you mean `=~`, just a little typo there I am assuming. – Reese Moore Nov 15 '10 at 03:50
  • 4
    you shouldn't really use Regex to parse XML or HTML. You should use a parser such as [Nokogiri](http://nokogiri.org). If you're just going to use regex anyway, you don't need to escape the `<` and `>`. The following will work just fine, `(.+)<\/name>`, – maček Nov 15 '10 at 04:15
  • 1
    As @macek said, parsing XML or HTML with regex is very fragile and error-prone. [This](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) is what will happen. – the Tin Man Nov 15 '10 at 04:33

2 Answers2

5

It is in the variable $1

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
3

Rather than use regex to parse XML or HTML, use a real parser. I like Nokogiri:

require 'nokogiri'

doc = Nokogiri::XML('<somecontainingtags><name>blah</name></somecontainingtags>')

# find all occurences
doc.search('//name').map {|n| n.inner_text } # => ["blah"]

# find the first occurance
doc.at('//name').inner_text # => "blah"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303