2017-08-11 55 views
4

到底包围的制表符,我会要所有的都内" 包围的\t我目前在Regex101想我正则表达式的各种迭代来代替......这是迄今为止最接近的...C#正则表达式 - 试图让所有的双引号

originString = blah\t\"blah\tblah\"\t\"blah\"\tblah\tblah\t\"blah\tblah\t\tblah\t\"\t\"\tbleh\" 
regex = \t?+\"{1}[^"]?+([\t])?+[^"]?+\" 
\t?+  maybe one or more tab 
\"{1}  a double quote 
[^"]?+  anything but a double quote 
([\t])?+ capture all the tabs 
[^"]?+  anything but a double quote 
\"{1}  a double quote 

我的逻辑是有缺陷的! 我需要你的帮助来分组制表符。

回答

4

如同一个单纯的"[^"]+"正则表达式的双引号字符串(如果没有转义序列占)并更换里面的标签只有内部的匹配评估匹配:

var str = "A tab\there \"inside\ta\tdouble-quoted\tsubstring\" some\there"; 
var pattern = "\"[^\"]+\""; // A pattern to match a double quoted substring with no escape sequences 
var result = Regex.Replace(str, pattern, m => 
     m.Value.Replace("\t", "-")); // Replace the tabs inside double quotes with - 
Console.WriteLine(result); 
// => A tab here "inside-a-double-quoted-substring" some here 

C# demo

+1

完美,比我想要做的更简单!谢谢! –

-1

您可以使用此:

\"[^\"]*\" 

最初回答here

相关问题