Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex validate either empty or only letters

Tags:

java

regex

I need to validate an input to be either empty or only contain letters. The letter part I got figured out at ^[a-zA-Z]+$ . How can I accept the input if it's empty too?

like image 656
Sorin Morovan Avatar asked Feb 19 '26 15:02

Sorin Morovan


1 Answers

Try changing your + to a *, as in this pattern:

^[a-zA-Z]*$

This will match zero or more Latin letters, so it will accept an empty string as well.

For international support, you might also want to consider:

^\p{L}*$

This will match zero or more letters in any Unicode language.

like image 55
p.s.w.g Avatar answered Feb 22 '26 05:02

p.s.w.g



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!