我怎样才能remove从字符串在C#中引用的字符串? [英] How can I remove quoted string literals from a string in C#?
本文介绍了我怎样才能remove从字符串在C#中引用的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串:
您好援引串和棘手的东西的世界
和想要得到的串减去报件返还。例如,
您好,世界
有什么建议?
解决方案
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
这篇关于我怎样才能remove从字符串在C#中引用的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文