用双引号替换所有出现的Tab字符 [英] Replace all occurrences of the Tab character within double quotes

查看:97
本文介绍了用双引号替换所有出现的Tab字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后,我要替换所有包含在 $ b中的 \t $ b我目前在 Regex101 上尝试使用正则表达式的各种迭代...这是我最近的一次远...

In the end, I will want to replace all the \t that are enclosed within " I'm currently on Regex101 trying various iterations of my regex... This is the the closest I have so far...

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

我的逻辑有缺陷!
我需要您对制表符进行分组。

My logic is flawed! I need your help in grouping the tab characters.

推荐答案

匹配双引号的子字符串仅使用 [^] + 正则表达式(如果没有要解释的转义序列)并替换选项卡insi仅在比赛评估程序内进行比赛:

Match the double quoted substrings with a mere "[^"]+" regex (if there are no escape sequences to account for) and replace the tabs inside the matches only inside a match evaluator:

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#演示

这篇关于用双引号替换所有出现的Tab字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆