1

I try to use the regex to check the username should start with letters and ends with at least one digit, but the following code only allows me to end with 1 digit, and I'm not sure about whether I used the * correctly, I think "[regex]*" means match at least one times [regex]

if( username.matches("^[a-zA-z]*\\d*$") ){
System.out.println("The username is valid");

}
else{
System.out.println("The username is invalid");

}
Mdev
  • 2,440
  • 2
  • 17
  • 25
Kesong Xie
  • 1,316
  • 3
  • 15
  • 35

1 Answers1

2

You were close:

^[a-zA-z]+[\d]+$

^[a-zA-z]+ make sure there is at least one character at the beginning of the string.

[\d]+$ make sure there is at least one digit at the end.

Dan Harms
  • 4,725
  • 2
  • 18
  • 28