开发者

Ruby RegEx to Match between . and :

I have this string: "51. some thing:"

and would like to extract just "some thing" using reg ex in ruby. Can anyone point me in the right direction to match between a 开发者_如何学编程. and a : and discard everything else?


'51. some thing:'.sub /.*\. *([^:]*).*/, '\1'
=> "some thing"


This should do it:

(?<=\.).+?(?=:)

If you want to avoid leading and trailing spaces in the match, you can use this:

(?<=\.\s*)\S.*?(?=\s*:)

Edit: I just checked and noticed that Ruby doesn't support lookbehind, which is used here. Alternatively, you can use this and take the capture of the regex as result:

\.\s*(\S.*?)\s*:
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜