简体   繁体   English

无法使用正则表达式在双引号之间提取字符串

[英]Unable to extract string between double quotes using regex

I am trying to extract those substrings that are enclosed in double quotes using regular expression: 我试图使用正则表达式提取用双引号括起来的子串:

"\w[\w\s\t]*"

on string: 在字符串上:

"@test" skip "2 3" skip "TEST" skip "te st" skip "@#" “@test”跳过“2 3”跳过“TEST”跳过“te st”跳过“@#”

Bolded substrings are successfully extracted. 已成功提取粗体子串。 But those with special characters are not extracted. 但是没有提取具有特殊字符的那些。 Please help me solve this. 请帮我解决这个问题。 I am not so pro in making regular expressions. 我不是那么专业地制作正则表达式。

This regex should work 这个正则表达式应该工作

"(.+?)"

Regex101 demo Regex101演示

It uses the concept of Group capture 它使用了组捕获的概念

As eckes said in his comment, try using 正如埃克斯在评论中所说,试着用

"[^"]*" “[^”] *”

This should match a quote, then any number of characters that aren't quotes, then another quote. 这应该匹配一个引号,然后匹配任何数量的非引号字符,然后是另一个引号。 The other answers will not match a 0-length, depending on if that's what you want. 其他答案将不匹配0长度,具体取决于您是否想要它。

string input = @"""@test"" skip ""2 3"" skip ""TEST"" skip ""te st"" skip ""@#""";
var values = Regex.Matches(input, @"\""(.+?)\""")
                  .Cast<Match>()
                  .Select(m => m.Groups[1].Value)
                  .ToList();

You can also match a substring containing escaped double quotes: 您还可以匹配包含转义双引号的子字符串:

Regex: ".+?(?<!\\\\)" 正则表达式: ".+?(?<!\\\\)"

Code: 码:

var txt1 = "\"This is \\\"some text\\\" to capture\" \"no other text\"";
var regex1 = new Regex(@""".+?(?<!\\)""", RegexOptions.IgnoreCase  | RegexOptions.CultureInvariant);
var c1 = regex1.Matches(txt1).Cast<Match>().Select(d => d.Value.Trim()).ToList();

Output: 输出:

"This is \"some text\" to capture"
"no other text"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM