2

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"
1
  • do you want to allow white spaces in the match? Commented Aug 8, 2014 at 18:23

4 Answers 4

2

You can use this regex:

^(?!\s+$).{1,150}$
Sign up to request clarification or add additional context in comments.

Comments

1

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*$

2 Comments

OP wants to allow white spaces also.
Edited to allow for "sandwiching" whitespace.
0

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

Comments

0

^(.*[^\s].*){1,150}$

Regular expression visualization

Debuggex Demo

1 Comment

This doesn't actually enforce the upper limit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.