Need regexp to find substring between two tokens

Yevgeny Simkin picture Yevgeny Simkin · Jan 28, 2009 · Viewed 19.9k times · Source

I suspect this has already been answered somewhere, but I can't find it, so...

I need to extract a string from between two tokens in a larger string, in which the second token will probably appear again meaning... (pseudo code...)

myString = "A=abc;B=def_3%^123+-;C=123;"  ;

myB = getInnerString(myString, "B=", ";" )  ;

method getInnerString(inStr, startToken, endToken){
   return inStr.replace( EXPRESSION, "$1");
}

so, when I run this using expression ".+B=(.+);.+" I get "def_3%^123+-;C=123;" presumably because it just looks for the LAST instance of ';' in the string, rather than stopping at the first one it comes to.

I've tried using (?=) in search of that first ';' but it gives me the same result.

I can't seem to find a regExp reference that explains how one can specify the "NEXT" token rather than the one at the end.

any and all help greatly appreciated.


Similar question on SO:

Answer

Evan Fosmark picture Evan Fosmark · Jan 28, 2009

You're using a greedy pattern by not specifying the ? in it. Try this:

".+B=(.+?);.+"