2017-08-11 57 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

相關問題