[英]Unable to extract string between double quotes using regex
我试图使用正则表达式提取用双引号括起来的子串:
"\w[\w\s\t]*"
在字符串上:
“@test”跳过“2 3”跳过“TEST”跳过“te st”跳过“@#”
已成功提取粗体子串。 但是没有提取具有特殊字符的那些。 请帮我解决这个问题。 我不是那么专业地制作正则表达式。
正如埃克斯在评论中所说,试着用
“[^”] *”
这应该匹配一个引号,然后匹配任何数量的非引号字符,然后是另一个引号。 其他答案将不匹配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();
您还可以匹配包含转义双引号的子字符串:
正则表达式: ".+?(?<!\\\\)"
码:
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();
输出:
"This is \"some text\" to capture"
"no other text"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.