以下正则表达式已在 regex101 上验证并且工作正常,匹配“()”、“[]”或“{}”:
\(\)|\[]|\{}
但是:
它的可读性不太好
在 Java 中变得更不可读:
\\(\\)|\\[]|\\{}
但仍然工作正常,正如我的测试课程所示。
现在我想通过使用 Unicode(应该避免转义)和常量来使其更具可读性,如下定义:
private static final String MATCH_OPENING_BRACE = "\u0028";
private static final String MATCH_CLOSING_BRACE = "\u0029";
private static final String MATCH_OPENING_SQUARE_BRACE = "\u005B";
private static final String MATCH_CLOSING_SQUARE_BRACE = "\u005D";
private static final String MATCH_OPENING_CURLY_BRACE = "\u007B";
private static final String MATCH_CLOSING_CURLY_BRACE = "\u007D";
private static final String MATCHING_OR_FLAG = "|";
private static final String COMPLETE_REGEX =
MATCH_OPENING_BRACE + MATCH_CLOSING_BRACE
+ MATCHING_OR_FLAG + MATCH_OPENING_SQUARE_BRACE + MATCH_CLOSING_SQUARE_BRACE
+ MATCHING_OR_FLAG + MATCH_OPENING_CURLY_BRACE + MATCH_CLOSING_CURLY_BRACE;
private static final String REGEX_REPLACEMENT = "";
这样我就可以编写这样的可读代码:
@Override
public boolean isValid(String input) {
for (int i = input.length() / 2; i > 0; i--)
input = input.replaceAll(COMPLETE_REGEX, REGEX_REPLACEMENT);
return input.isEmpty();
}
而不是使用不可读的文字,如下所示:
@Override
public boolean isValid(String input) {
for (int i = input.length() / 2; i > 0; i--)
input = input.replaceAll("\\(\\)|\\[]|\\{}", "");
return input.isEmpty();
}
但是这里抛出了以下异常:
java.util.regex.PatternSyntaxException: Unclosed character class near index 7
()|[]|{}
^
我尝试添加转义字符,如下所示:
private static final String MATCH_OPENING_CURLY_BRACE = "\\\u007B";
但这只给出了类似的例外:
java.util.regex.PatternSyntaxException: Unclosed character class near index 8
()|[]|\{}
^
有什么提示吗?
正如@user85421提到的,使用unicodes不会使转义变得不必要, 正如我所想的那样。
所以,转义 (, ), [ 和 { 仍然是必需的,这是修复方法:
private static final String MATCH_OPENING_BRACE = "\\\u0028";
private static final String MATCH_CLOSING_BRACE = "\\\u0029";
private static final String MATCH_OPENING_SQUARE_BRACE = "\\\u005B";
private static final String MATCH_CLOSING_SQUARE_BRACE = "\u005D";
private static final String MATCH_OPENING_CURLY_BRACE = "\\\u007B";
private static final String MATCH_CLOSING_CURLY_BRACE = "\u007D";