我有一个字符串:
您好援引串和棘手的东西的世界
和想要得到的串减去报件返还。例如,
您好,世界
有什么建议?
解决方案 resultString = Regex.Replace(subjectString,
@(['])#匹配报价,请记住哪一个
(?: # 然后...
(?!\ 1)#(只要下一个字符是不一样的报价与以前一样)
。 #匹配任何字符
)*#任何次数
\ 1#,直到相应的收盘报价
\ S *#加上可选空白
,
,RegexOptions.IgnorePatternWhitespace);
将工作在你的榜样。
resultString = Regex.Replace(subjectString,
@(['])#匹配报价,请记住哪一个
(?: # 然后...
(?!\ 1)#(只要下一个字符是不一样的报价与以前一样)
\\? #匹配任何转义或转义字符
)*#任何次数
\ 1#,直到相应的收盘报价
\ S *#加上可选空白
,
,RegexOptions.IgnorePatternWhitespace);
也将处理转义引号。
,以便正常转换
你好援引\字符串\\和棘手的东西的世界
到
您好,世界
I have a string:
Hello "quoted string" and 'tricky"stuff' world
and want to get the string minus the quoted parts back. E.g.,
Hello and world
Any suggestions?
解决方案resultString = Regex.Replace(subjectString,
@"([""'])# Match a quote, remember which one
(?: # Then...
(?!\1) # (as long as the next character is not the same quote as before)
. # match any character
)* # any number of times
\1 # until the corresponding closing quote
\s* # plus optional whitespace
",
"", RegexOptions.IgnorePatternWhitespace);
will work on your example.
resultString = Regex.Replace(subjectString,
@"([""'])# Match a quote, remember which one
(?: # Then...
(?!\1) # (as long as the next character is not the same quote as before)
\\?. # match any escaped or unescaped character
)* # any number of times
\1 # until the corresponding closing quote
\s* # plus optional whitespace
",
"", RegexOptions.IgnorePatternWhitespace);
will also handle escaped quotes.
So it will correctly transform
Hello "quoted \"string\\" and 'tricky"stuff' world
into
Hello and world