simple regex [a-z] work for letter but fails on Word
Question
I am writing a simple regex validator method in nuxeo java
mystring.matches("[a-z]")
This validate correctly if enter any letter . a or b or z .
This validation allows to enter a letter but when i enter a word it fails.
Why is that ? Do i have to enter any length param ?
Solution
You only allow a single match of a character. "[a-z]+" would let 1 to N characters of lowercase to pass.
OTHER TIPS
[a-z]
means match one character in the range a-z. If you want to match an arbitrary number of characters, 0 or more, you can use [a-z]*
. If you want to match one character or more, [a-z]+
, or if you want to be more specific, [a-z]{4}
matches only 4 characters while [a-z]{4,6}
matches 4, 5, or 6 characters.
See this article on repetition quantifiers for more information.