tags:

views:

34

answers:

2

In Ruby, I'm trying to extract from text a string that starts 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

+2  A: 

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

Try this regex :

who.*?(?=when\b|\))
Thibault Falise
Perfect. Many thanks.
Alex Hamilton
A: 

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

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

Lazarus