1

I'm looking to capture just the number after vspace. How would you do that in regex? Here is a sample string.

<img align="left" alt="dude its me" border="10" height="168" hspace="30"  vspace="10" width="130" />

So the end result would be 10

Trip
  • 26,756
  • 46
  • 158
  • 277

4 Answers4

2
>> s = '<img align="left" alt="dude its me" border="10" height="168" hspace="30"  vspace="10" width="130" />'
>> /vspace="(\d+)"/.match(s)[1]
=> "10"

or, if you're not sure if it exists or not:

if /vspace="(\d+)"/ =~ s
  puts $1
else
  puts "no match"
end
Peter
  • 127,331
  • 53
  • 180
  • 211
  • 2
    It's probably more readable to use s.match(/.../) which follows the Ruby convention than /.../ =~ s which is inherited from Perl and looks a lot more like line-noise to those not familiar with what it means. – tadman Jul 27 '10 at 20:36
2

Keeping in mind that the vspace could be specified with single quotes, double quotes or no quotes.

n = Nokogiri::HTML(%Q{<img align="left" alt="dude its me" border="10" height="168" hspace="30"  vspace="10" width="130" />})
n.css("img").first["vspace"]

Never ever parse HTML with regular expressions.

Community
  • 1
  • 1
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
1

To capture just the 10 try this one: (?=\bvspace=")?(\d+)

/vspace="(\d+)" should match the entirety of vspace="10"

AllenG
  • 8,112
  • 29
  • 40
0
/vspace="(\d+)"/$1/
Toto
  • 89,455
  • 62
  • 89
  • 125
  • 1
    Hmm. that last piece with the $1. Not sure I understand that. – Trip Jul 27 '10 at 19:27
  • $1 content what is between parenthesis i.e. 10 with your example. I don't know RoR regex but i presume there is something similar. – Toto Jul 27 '10 at 19:37