开发者

matching a string in ruby with regex

I have the following line

'passenger (2.2.5, 2.0.6)'.match(//)[0]

which obviousl开发者_运维技巧y doesn't match anything yet

I want to return the just the content of (2.2.5, so everything after the open parentheses and before the comma.

How would I do this?


Beanish solution fails on more than 2 version numbers, you should use something like:

>> 'passenger (2.2.5, 2.0.6, 1.8.6)'.match(/\((.*?),/)[1] # => "2.2.5"


'passenger (2.2.5, 2.0.6)'.match(/\((.*),/)[1]

if you use the $1 element it is the group that is found within the ( )


#!/usr/bin/env ruby

s = 'passenger (2.2.5, 2.0.6)'
p s.scan(/(?:\(|, *)([^,)]*)/).flatten    # => ["2.2.5", "2.0.6"]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜