這將字符串工作不包含轉義引號(因爲這些會拋出計數爲偶數的報價表外):
Regex regexObj = new Regex(
@"\w+~[\d.]* # Match an alnum word, tilde, optional digits/dots
(?= # only if there follows...
[^""]* # any number of non-quotes
(?: # followed by...
""[^""]* # one quote, and any number of non-quotes
""[^""]* # another quote, and any number of non-quotes
)* # any number of times, ensuring an even number of quotes
[^""]* # Then any number of non-quotes
$ # until the end of the string.
) # End of lookahead assertion",
RegexOptions.IgnorePatternWhitespace);
如果轉義引號需要解決的問題,它有點複雜:
Regex regexObj = new Regex(
@"\w+~[\d.]* # Match an alnum word, tilde, optional digits/dots
(?= # only if there follows...
(?:\\.|[^\\""])* # any number of non-quotes (or escaped quotes)
(?: # followed by...
""(?:\\.|[^\\""])* # one quote, and any number of non-quotes
""(?:\\.|[^\\""])* # another quote, and any number of non-quotes
)* # any number of times, ensuring an even number of quotes
(?:\\.|[^\\""])* # Then any number of non-quotes
$ # until the end of the string.
) # End of lookahead assertion",
RegexOptions.IgnorePatternWhitespace);
您需要使用負向後視和前視。 Google他們。 – Barmar
這很複雜。我假設你也不想匹配'「foo吧〜10 baz」',對嗎?那麼,它可以在正則表達式中完成,但是我想首先知道引用的字符串是否可以包含轉義引號(如'\「')? –
@Barmar:對於不知道正則表達式,谷歌搜索不會幫他在這裏,StackOverflow可以 –