Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need regexp to find substring between two tokens

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:

  • Regex: To pull out a sub-string between two tags in a string
  • Regex to replace all \n in a String, but no those inside [code] [/code] tag
  • Replace patterns that are inside delimiters using a regular expression call
  • RegEx matching HTML tags and extracting text
like image 460
Yevgeny Simkin Avatar asked Dec 10 '22 22:12

Yevgeny Simkin


2 Answers

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

".+B=(.+?);.+" 
like image 65
Evan Fosmark Avatar answered May 16 '23 08:05

Evan Fosmark


Try this:

B=([^;]+);

This matches everything between B= and ; unless it is a ;. So it matches everything between B= and the first ; thereafter.

like image 25
Gumbo Avatar answered May 16 '23 06:05

Gumbo