1

I'm trying to create a regular expression with the below restrictions

  1. Allow any character '\w\W\s' (e.g rand123#!#@adfads)
  2. Disallow only numbers (e.g 12312312)
  3. Disallow only non alphanumeric characters (e.g !@#$$#%#^%$^%)
  4. Number of characters must be between 3 to 60

Following along the lines of this answer, but could not get it work.

^(?=.{3,60}$)(?![\W_]*$)(?![0-9]*$)[\w\W\s]+$
2

1 Answer 1

1

Note that \W matches \s, so '\w\W\s' can be reduced to [\w\W].

You may use 2 negative lookaheads anchored at the start to impose two "disallow-only" conditions like this:

^(?![\W_]+$)(?![0-9]+$)[\w\W]{3,60}$

See the regex demo

Pattern details:

  • ^ - start of string
  • (?![\W_]+$) - the string cannot consist of non-alphanumeric chars
  • (?![0-9]+$) - the string cannot consist of digits only
  • [\w\W]{3,60} - 3 to 60 any characters
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.