如果你想要一个正则表达式,这很容易完成:
boolean oddQuotes = subjectString.matches("[^\"]*\"(?:[^\"]*\"[^\"]*\")*[^\"]*");
说明:(没有所有 Java 引用转义):
[^"]*" # Match any number of non-quote characters, then a quote
(?: # Now match an even number of quotes by matching:
[^"]*" # any number of non-quote characters, then a quote
[^"]*" # twice
)* # and repeat any number of times.
[^"]* # Finally, match any remaining non-quote characters
到目前为止,这可能比简单的“计算引号”解决方案要慢。但我们可以做得更好:我们可以设计正则表达式来处理转义的引号,即如果引号前面有奇数个反斜杠,则不计算引号:
boolean oddQuotes = subjectString.matches("(?:\\\\.|[^\\\\\"])*\"(?:(?:\\\\.|[^\\\\\"])*\"(?:\\\\.|[^\\\\\"])*\")*(?:\\\\.|[^\\\\\"])*");
现在诚然,这看起来很可怕,但主要是因为 Java 的字符串转义规则。实际的正则表达式很简单:
(?: # Match either
\\. # an escaped character
| # or
[^\\"] # a character except backslash or quote
)* # any number of times.
" # Then match a quote.
(?: # The rest of the regex works just the same way (as above)
(?:\\.|[^\\"])*"
(?:\\.|[^\\"])*"
)*
(?:\\.|[^\\"])*