这适用于不包含转义引号的字符串(因为这些字符串会使偶数引号的计数失去平衡):
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);