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?
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: