【问题标题】:Replace all occurrences of the Tab character within double quotes替换双引号内所有出现的制表符
【发布时间】:2018-01-20 03:06:46
【问题描述】:

最后,我想替换 " 中包含的所有 \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


    【解决方案1】:

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

    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

    【讨论】:

    • 完美,比我尝试做的简单得多!谢谢!
    【解决方案2】:

    你可以用这个:

    \"[^\"]*\"
    

    最初回答here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 2015-10-04
      • 2013-04-15
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多