开发者

Regular expression ending with the first of two options

开发者 https://www.devze.com 2023-01-03 01:18 出处:网络
In Ruby, I\'m trying to extract from text a string that starts开发者_如何学JAVA with \"who\" and ends with the first of either a \")\" or \"when\" (but doesn\'t include the \")\" or the \"when\").

In Ruby, I'm trying to extract from text a string that starts开发者_如何学JAVA with "who" and ends with the first of either a ")" or "when" (but doesn't include the ")" or the "when").

I've tried the following:

who.*(?=when\b|\))

which fails the case where it finds both a "when" and a ")".

Any ideas?

Many thanks


You need to make your .* part non-greedy.

Try this regex :

who.*?(?=when\b|\))


(who.*?)(when|\))

That should do it I think. $1 will be your string.

0

精彩评论

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