替换双引号内所有出现的 Tab 字符

bla*_*125 5 c# regex regex-group

最后,我想替换 我目前在Regex101上的所有\t包含的内容,尝试对我的正则表达式进行各种迭代......这是我迄今为止最接近的......"

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
Run Code Online (Sandbox Code Playgroud)

我的逻辑有问题!我需要您帮助对制表符进行分组。

Wik*_*żew 6

将双引号子字符串与纯正"[^"]+"则表达式匹配(如果没有要考虑的转义序列),并仅在匹配评估器中替换匹配项内的制表符:

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
Run Code Online (Sandbox Code Playgroud)

参见C# 演示

  • 完美而且比我想做的要简单得多!谢谢! (2认同)