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");

}
Matthew
  • 2,389
  • 2
  • 15
  • 24
Kesong Xie
  • 1,174
  • 1
  • 11
  • 31

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
  • 3,911
  • 2
  • 16
  • 26