I'm very new to programming and I've been told to avoid regex for now, but I find it extremely helpful.
When writing a program to check if a string only contains letters, I found on stackoverlow that both ^[a-zA-Z]+$
and [a-zA-Z]*
yield the same results. I understand how [a-zA-Z]
works and I understand how [A-z]
is different from both of those as well, but I do not understand +$ vs ^[]* or why they yield the same result and I'm having trouble finding anything to explain it.
Here's the example I used it in:
String student = input.next();
while (!student.matches("[a-zA-Z]*")) {
System.out.print("Invalid input. Enter name: ");
student = input.next();
}
This is my first question here so sorry if this kind of question is frowned upon.