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

问题描述 投票:4回答:2

最后,我要替换包含在\t中的所有"我目前正在Regex101上尝试我的正则表达式的各种迭代...这是我到目前为止最接近的迭代器...

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

我的逻辑有缺陷!我需要您对标签字符进行分组的帮助。

c# regex regex-group
2个回答
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
投票

您可以使用此:

\"[^\"]*\"

最初回答here

© www.soinside.com 2019 - 2024. All rights reserved.