Bhuwan Paneru
Bhuwan Paneru

Reputation: 3

I have regex but its not catch angle bracket

I have regex :

[RegularExpression(@"^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,40}$", 
                   ErrorMessage = "Minimum 8, max 40 characters atleast 1 Uppercase, 1 Lowercase, 1 Number and 1 Special Character")]

I want no special characters like <, >(angle brackets). So can anyone help me?

Upvotes: 0

Views: 166

Answers (1)

41686d6564
41686d6564

Reputation: 19641

You may use the following pattern:

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W)[^\s<>]{8,40}$

Demo.

Note that your original pattern had some redundant parts. For example, the capturing groups, some of the character classes, and the {1,} quantifiers; so, they were removed.

Breakdown of the pattern:

  • ^ - Beginning of the string.
  • (?=.*[A-Z]) - Must contain at least one uppercase letter.
  • (?=.*[a-z]) - Must contain at least one lowercase letter.
  • (?=.*\d) - Must contain at least one digit.
  • (?=.*\W) - Must contain at least one non-word character.
  • [^\s<>]{8,40} Match between 8 and 40 characters excluding whitespace characters and angle brackets.
  • $ - End of the string.

Upvotes: 1

Related Questions