我有一个字符串文本,就像:
"ruf": "the text I want",
"puf":我要把引号里的文字提取出来。
试过这个:
string cg="?<=\"ruf\":\")(.*?)(?=\",puf";
Regex g = new Regex(cg);它不起作用。
发布于 2014-07-12 11:32:26
试试下面的regex:
(?<="ruf":\s\")[^"]*在线演示
用于程序的字符串文字:
C#
@"(?<=""ruf"":\s\"")[^""]*"产出:
the text I want模式描述:
(?<= 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))

编辑
你能加点puf吗。因为它是一个长文本,其中包含多引号。
如果您正在寻找"puf“,请在regex下面试一试:
(?<="ruf":\s\")[\s\S]*(?=",\s*"puf")在线演示
用于程序的字符串文字:
C#
@"(?<=""ruf"":\s\"")[\s\S]*(?="",\s*""puf"")"发布于 2014-07-12 11:33:42
您可以使用s修饰符尝试下面的正则表达式,
/(?<=\"ruf\": \")[^\"]*(?=\",.*?\"puf\":)/s演示
使用s修饰符,点也匹配甚至换行符。
发布于 2014-07-12 11:44:10
这样做吧:
var myRegex = new Regex(@"(?s)(?<=""ruf"": "")[^""]*(?=\s*""puf"")");
string resultString = myRegex.Match(yourString).Value;
Console.WriteLine(resultString);https://stackoverflow.com/questions/24712443
复制相似问题