views:

45

answers:

4

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

+1  A: 
/vspace="(\d+)"/$1/
M42
Hmm. that last piece with the $1. Not sure I understand that.
Trip
$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.
M42
Why the downvote ?
M42
+1  A: 
>> 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
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
+1  A: 

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

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

AllenG
+1  A: 

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.

Ryan Bigg