Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# regex获取包含双引号的文本_C#_Regex - Fatal编程技术网

C# regex获取包含双引号的文本

C# regex获取包含双引号的文本,c#,regex,C#,Regex,什么是正则表达式以获取双引号内的文本 我的正则表达式是: "\"([^\"]*)\"" 示例:“我需要这个” 输出:我需要这个 我得到了:“我需要这个”以下是您问题的完整解决方案: string sample = "this is \"what I need\""; Regex reg = new Regex(@"""(.+)"""); Match mat = reg.Match(sample); string foundValue = ""; if(mat.Groups.Count

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

我的正则表达式是:

  "\"([^\"]*)\""
示例:
“我需要这个”

输出:
我需要这个


我得到了:
“我需要这个”
以下是您问题的完整解决方案:

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);
印刷品:

我需要什么


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

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);
印刷品:

我需要什么


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

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

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

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

(?发布答案太晚了

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();
string text=“some text\”我需要这个\“\”还有这个\“但不是这个”;
List matches=Regex.matches(文本,@“”(.+?)“”“”)Cast()
.Select(m=>m.Groups[1]。值)
.ToList();

发布答案太晚了吗

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();
string text=“some text\”我需要这个\“\”还有这个\“但不是这个”;
List matches=Regex.matches(文本,@“”(.+?)“”“”)Cast()
.Select(m=>m.Groups[1]。值)
.ToList();

这是正确的答案,但是你能解释一下为什么它有效吗(他们只想打印组,而不是整个匹配)?原始正则表达式同样有效,不必求助于
*
(或者在本例中是
+
)。但是+1指出需要的是
组[1]
而不是
组[0]
。我怀疑这就是OP程序的问题所在。这是正确的答案,但你能解释一下为什么它能工作(他们只想打印组,而不是整个匹配)?原始正则表达式也能工作,并且不必求助于
*
(或者在本例中是
+
)。但是+1指出需要的是
组[1]
而不是
组[0]
。我怀疑这是OP程序的问题。这很有帮助,但是,我也得到了引号后面的文本“…请您更改它…谢谢示例:-“一切都很好”输出:-一切都很好,这也即将到来。。Thanks@bhargava奇怪的是,我不明白这一点。问题是,前瞻不使用结束引号。正则表达式开始在第一个匹配结束的位置查找第二个匹配,并几乎立即找到一个,因为前瞻匹配相同的qu请注意,前面的前瞻匹配。这很有帮助,但是,我也会在引号“”后得到文本…请您更改它…谢谢示例:-“一切都很好”输出:-一切都很好,这也即将到来。。Thanks@bhargava奇怪的是,我不明白这一点。问题是,前瞻不使用结束引号。正则表达式开始在第一个匹配结束的位置查找第二个匹配,并几乎立即找到一个,因为前瞻匹配相同的qu请注意,前面的先行匹配。