Reputation: 65
I have a string text which is like:
"ruf": "the text I want",
"puf":
I want extract the text inside the quotes.
tried this :
string cg="?<=\"ruf\":\")(.*?)(?=\",puf";
Regex g = new Regex(cg);
It didnt work.
Upvotes: 0
Views: 51
Reputation: 46871
Try with below regex:
(?<="ruf":\s\")[^"]*
String literals for use in programs:
C#
@"(?<=""ruf"":\s\"")[^""]*"
output:
the text I want
Pattern description:
(?<= look behind to see if there is:
"ruf": '"ruf":'
\s whitespace (\n, \r, \t, \f, and " ")
\" '"'
) end of look-behind
[^"]* any character except: '"' (0 or more times
(matching the most amount possible))
Can you add puf. Because it is a long text which has multiple quotes in it
If you are looking till "puf" is found then try below regex:
(?<="ruf":\s\")[\s\S]*(?=",\s*"puf")
String literals for use in programs:
C#
@"(?<=""ruf"":\s\"")[\s\S]*(?="",\s*""puf"")"
Upvotes: 3
Reputation: 41848
Do it like this:
var myRegex = new Regex(@"(?s)(?<=""ruf"": "")[^""]*(?=\s*""puf"")");
string resultString = myRegex.Match(yourString).Value;
Console.WriteLine(resultString);
Upvotes: 1
Reputation: 174834
You could try the below regex with s modifier,
/(?<=\"ruf\": \")[^\"]*(?=\",.*?\"puf\":)/s
With the s
modifier, dot matches even newline character also.
Upvotes: 1