0

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 ?

zod
  • 12,092
  • 24
  • 70
  • 106

2 Answers2

4

You only allow a single match of a character. "[a-z]+" would let 1 to N characters of lowercase to pass.

sjakubowski
  • 2,913
  • 28
  • 36
2

[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.

kba
  • 19,333
  • 5
  • 62
  • 89