Reputation: 1069
I have a problem about how to extract string between two string start with known string plus any unknown string in C#. My question may be confusing. Here's the problem.
This is the words start with "known_value_1"
unknown-here
"The words I want to extract"unknown-here
"known_value_2"
So, the two known values and the words I want to extract is inside the double quotes. The problem is, before and after the words to extract are unknown values but with no double quotes.
This doesn't work
string value = Regex.Match(input, "\"known_value_1\" \"(.*?)\" \"known_value_2\"").Groups[1].Value;
I have no idea about before and after \"(.*?)\"
to be unkonwn\"(.*?)\"unkonwn
. The unknown value is not in double quote.
Thank you in advance
Upvotes: 0
Views: 1870
Reputation: 2604
Put a non-greedy match anything - .*?
as unknown.
string value = Regex.Match(input, "\"known_value_1\".*?\"(.*?)\".*?\"known_value_2\"").Groups[1].Value;
Upvotes: 3