0

什么是正则表达式来获取双引号内的文本。

我的正则表达式是:

  "\"([^\"]*)\""

例子:"I need this"

输出:I need this

我正进入(状态:"I need this"

4

3 回答 3

3

这是您问题的完整解决方案:

string sample = "this is \"what I need\"";
Regex reg = new Regex(@"""(.+)"""); 
Match mat = reg.Match(sample);

string foundValue = "";
if(mat.Groups.Count > 1){
   foundValue = mat.Groups[1].Value;
}
Console.WriteLine(foundValue);

印刷:

我需要的

于 2013-05-10T16:24:28.447 回答
0

使用以下正则表达式,您无需任何分组即可获得所需内容

(?<=")[^"]+?(?=")

获取引用文本的代码:

string txt = "hi my name is \"foo\"";
string quotedTxt = Regex.Match(txt, @"(?<="")[^""]+?(?="")").Value;
于 2013-05-10T16:33:53.003 回答
0

发布答案太晚了?

string text = "some text \"I need this\"  \"and also this\" but not this";

List<string> matches = Regex.Matches(text, @"""(.+?)""").Cast<Match>()
                       .Select(m => m.Groups[1].Value)
                       .ToList();
于 2013-05-10T16:58:41.253 回答