开发者

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"  vs开发者_如何学Gopace="10" width="130" />

So the end result would be 10


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


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.


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜