user1759477
user1759477

Reputation: 39

regular expression:match a word and a length

I need help to do this regex. The sentence can't be less than 4 letters, and it can't match any of these word (test1,test2 and test3)

I know how to do each one separately but not together.

First condition ^.{4,}$

Second condition ^((?!test1|test2|test3).)*$

How to do both so that:-

  1. "hello" will pass
  2. "hel" will fail
  3. "test1" will fail although it is more than 4 letters long
  4. "test2" will fail although it is more than 4 letters long
  5. "test3" will fail although it is more than 4 letters long

Thanks in advance

Upvotes: 2

Views: 130

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Try this one:

(?=^.{4,}$)(^((?!test1|test2|test3).)*$)

Or:

(?=^.{4,}$)(^((?!test(1|2|3)).)*$)

Or:

(?=^.{4,}$)(^((?!test[1-3]).)*$)

Upvotes: 5

xdazz
xdazz

Reputation: 160843

Use the below:

/^(?!.*test[1-3]).{4,}$/

Upvotes: 1

Related Questions