3

I am trying to create a regular expression where the user should enter just a word or words with space between them and its length must have a limit [1 to 32].

For instance, it should accept below line;

wordX[space]wordX[space][space][space]wordX

but it should not accept that:

[space]wordX or wordX[space] or only [space]

wordX must be compatible with this regex:

^(([0-9A-Za-z!"#$%'()*+,./:;=?@^_`{|}~\\\[\]-]))$

1 Answer 1

1

Try this:

^(?:\w+\s){0,31}\w+$

\w means: digits, letters, underscore

For strictly accepted chars:

^(?:[\w!"#$%'()*+,./:;=?@^_`{|}~\\\[\]]+\s){0,31}[\w!"#$%'()*+,./:;=?@^_`{|}~\\\[\]]+$

Or:

^(?:\S+\s){0,31}\S+$

\S - any character other than whitespace

Update:

If word length is between 1 and 32 than use this regex:

^(?:\w{1,31}\s)*\w{1,31}$

In the same manner you can modify other regexes.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks but limit is not working. I tested on this page: tools.netshiftmedia.com/regexlibrary
just anything more than 32 characters. it accepts any word without limit.
@onuR, Is it word length limit or words count limit?
thanks a lot. I have one more issue, how can check total length of the phrase not only a word?
@onuR, Try this ^(?=.{40,100}$)(?:\w{1,31}\s)*\w{1,31}$ From 40 to 100 chars. I don't know, does your regex engine support lookahead...

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.