Reputation: 57
I'm trying to build a regular expression that doesn't allow only whitespaces, but, for example, does allow
" aaaaa "
" aaaaa"
"aaaaaa "
The string's lenght should be {1,150}.
I'm trying to use
^(?=.\\S).{1,20}$
...but it doesn't work for the input of
" aaaaaa"
Upvotes: 2
Views: 465
Reputation: 2900
I would check the contents and length separately. Apply a regex to check if the string contains at least one non-whitespace character at any position:
\S
And then check the length with:
myString.length() >= 1 && myString.length() <= 150
Upvotes: 0
Reputation: 2281
This is a regular expression to match between 1 and 150 non-whitespace characaters that are optionally sandwiched with whitespace:
^\s*\S{1,150}\s*$
Upvotes: 1