soren.qvist
soren.qvist

Reputation: 7416

Matching everything until a series of characters, without the series itself?

I'm trying to extract a portion of text from a string. I need to grab everything before the characters " (test)" first appear (Note the whitespace before the first parentheses), but excluding the " (test)" portion itself.

The string I'm testing this on:

"14 asdasddasddasdas twetwe .e.wrwe r342#¤¤¤¤ 822 (test) (test) (test)"

I'm using the pattern ^.+(?= \(test\)), but apparently this matches everything until the last " (test)" occurrence. I would like it to match the string:

 "14 asdasddasddasdas twetwe .e.wrwe r342#¤¤¤¤ 822 "

What am I doing wrong here?

Upvotes: 0

Views: 89

Answers (1)

ruakh
ruakh

Reputation: 183321

The + symbol means "one or more times — and preferably as many times as possible". (It's "greedy".) You need to use the special "nongreedy" or "lazy" equivalent, +?, which means "one or more times — and preferably as few times as possible". (Note: this is not to be confused with the use of ? to mean "zero or one times — and better one time than zero".) So, your pattern would be:

^.+?(?= \(test\))

Upvotes: 1

Related Questions