user532104
user532104

Reputation: 1423

Need a regex for a string that must have letters but can't have purely numbers and cant have special chars

Need a regex for string such that:

  1. Can have letters, numbers
  2. ignore special characters like: < > { } [ ] # ~ ^/\"
  3. Can't have purely numbers
  4. Max size is 20 chars

Valid answers a) "hello world 123 -" b) "123 hello - world" c) "- hello 123 world"

Invalid answers a) "123456" b) "123456 " c) "abc>>>" c) "abc123>>>" d) ">>>>" The closest i've come up with is:

^([A-Za-z0-9 '\-!\£\$:;%&amp;\*\(\)_=\+,.\?@\n\r\t]{1,20}$

But point 3 of "Can't have purely numbers" becomes invalid.

Any idea?

Upvotes: 1

Views: 104

Answers (1)

anubhava
anubhava

Reputation: 785008

You can use this lookahead based regex:

^(?![0-9]+$)(?!.*?[<>{}\[\]#~^\/"]).{1,20}$

^ - Line start
(?![0-9]+$) - Not just numbers
(?!.*?[<>{}\[\]#~^\/"]) - Doesn't have these special characters
.{1,20} - 1 to 20 characters
$ - Line end

Upvotes: 1

Related Questions