I want to write a regex where a string has
(9 characters) and start with either “g” or “r”
and then are all numbers afterward.
I have written this but it does not work:
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");
}
}
Corrected re:
You need not
+when you already has{8}.Also you don’t need
()when you don’t want to use the group further in the code.