Jeremy
Jeremy

Reputation: 46322

Get value between two substrings using regex

If I have a string "Param1=value1;Param2=value2;Param3=val3", how can I get the value between the substrings "Param2=" and the next semicolon (or end of string, whichever comes first)?"

Upvotes: 0

Views: 1127

Answers (4)

Jeremy
Jeremy

Reputation: 6670

You can use this string to place all of the values into the Matches collection of the Regex class

string regex = "Param[0-9]*?=(?<value>.*?)(;|$)"

Upvotes: 0

Chas. Owens
Chas. Owens

Reputation: 64909

You can either use a character class that excludes ; (as others have answered), or you can use a non-greedy match anything:

/Param2=(.*?);/

Upvotes: 0

Shea
Shea

Reputation: 11243

"Param\d+=([^;]*)" will capture the contents between = and ; in group 1

Upvotes: 1

Martijn Laarman
Martijn Laarman

Reputation: 13536

/Param2=([^;]+)/

Upvotes: 3

Related Questions