开发者

Ruby RegEx to Match between . and :

开发者 https://www.devze.com 2023-01-31 08:05 出处:网络
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

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

精彩评论

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