如果我希望用户匹配,正则表达式是什么。“abc-1234”
所以 3 个字母(大写或小写)后跟 4 个数字...
这是我到目前为止所拥有的(非常感谢!):
if (id.matches("^[A-Z][a-zA-Z]{3}-[\\d]{4}$")) {
this.id = id;
} else {
throw new IllegalArgumentException("Inventory ID must be in the "
+ "form of ABC-1234");
}
}
最佳答案
Pattern
Javadoc说(编辑/部分)
Predefined character classes
\d A digit: [0-9]
POSIX character classes (US-ASCII only)
\p{Lower} A lower-case alphabetic character: [a-z] \p{Upper} An upper-case alphabetic character:[A-Z] \p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
所以你可以使用类似的东西
if (id.matches("\\p{Alpha}{3}-\\d{4}")) {
关于java - JAVA 中 "ABC-1234"的正则表达式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37222358/