正则表达式问题

3 java regex string

我想写一个正则表达式,其中一个字符串有(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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Igo*_*bin 6

更正了:

"^[gr]([0-9]{8})$"
Run Code Online (Sandbox Code Playgroud)

+当你已经拥有时,你不需要{8}.

你也不必()当你不想再使用该组中的代码.

"^[gr][0-9]{8}$"
Run Code Online (Sandbox Code Playgroud)