开发者

Regular expression ending with the first of two options

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜