我想写一个正则表达式,其中一个字符串有 (9 个字符)并以“g”或“r”开头 然后后面都是数字。
我已经写了这个但是它不起作用:
public static void main(String[] args) {
String id= "g57895452";
String pattern = "/^g([0-9]+){8}$/";
if (id.matches(pattern)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
最佳答案
更正:
"^[gr]([0-9]{8})$"
当您已经拥有 {8}
时,您不需要 +
。
此外,当您不想在代码中进一步使用该组时,您也不需要 ()
。
"^[gr][0-9]{8}$"
关于java - 正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11308825/