Trip
Trip

Reputation: 27114

Regex - Capturing a number if a match exists

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

Upvotes: 1

Views: 976

Answers (4)

Ryan Bigg
Ryan Bigg

Reputation: 107718

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.

Upvotes: 2

AllenG
AllenG

Reputation: 8190

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

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

Upvotes: 1

Peter
Peter

Reputation: 132157

>> 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

Upvotes: 2

Toto
Toto

Reputation: 91375

/vspace="(\d+)"/$1/

Upvotes: 0

Related Questions