-2

Regular Expression To exclude sub-string name(job corps)
Includes at least 1 upper case letter, 1 lower case letter, 1 number and 1 symbol except "@"

I have written something like below :

^((?!job corps).)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*]).*$

I tested with the above regular expression, not working for special character.

can anyone guide on this..

Kapil
  • 1,823
  • 6
  • 25
  • 47
  • To exclude `job corps`, use `^(?!.*job corps)`, not `^((?!job corps).)`. What do you mean by "not working"? What is the example string? Try `^(?!.*job corps)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*])[^@]*$` or `^(?!.*job corps)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*])(?!.*@).*$`. – Wiktor Stribiżew Nov 06 '15 at 09:19
  • replace the final `.*` with `[^@]*` or an other character class (with only allowed characters) that excludes the `@`. – Casimir et Hippolyte Nov 06 '15 at 09:20
  • I have tried your one..not working for atleast one capital for rest of the condition it's working fine. – Kapil Nov 06 '15 at 09:27
  • Your regex does not match `Lipak@123`, do you want to match it? BTW, [`Lipak@123!`](http://regexstorm.net/tester?p=%5e(%3f!.*job+corps)(%3f%3d.*%5b0-9%5d)(%3f%3d.*%5ba-z%5d)(%3f%3d.*%5bA-Z%5d)(%3f%3d.*%5b!%23%24%25%5e%26*%5d).*%24&i=Lipak%40123!) is matched as it has all the required symbols. – Wiktor Stribiżew Nov 06 '15 at 09:27
  • lipak&123 should not match because Upper case is missing.BTW I need to accept all the symbols except @ – Kapil Nov 06 '15 at 09:30
  • These strings you post in comments [do not match](http://regexstorm.net/tester?p=%5e(%3f!.*job+corps)(%3f%3d.*%5b0-9%5d)(%3f%3d.*%5ba-z%5d)(%3f%3d.*%5bA-Z%5d)(%3f%3d.*%5b!%23%24%25%5e%26*%5d).*%5cr%3f%24&i=Lipak%40123!%0d%0alipak%26123%0d%0alipak%40123&o=m) with `^(?!.*job corps)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*]).*$`. You must be using `RegexOptions.IgnoreCase` – Wiktor Stribiżew Nov 06 '15 at 09:33

2 Answers2

0

If I understand well your requirements, you can use this pattern:

^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[^@]*$

If you only want to allow characters from [a-zA-Z0-9^#$%&*] changes the pattern to:

^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[a-zA-Z0-9^#$%&*]*$

details:

^ # start of the string
(?! # not followed by any of these cases
    [^a-z]*$  # non lowercase letters until the end
  |
    [^A-Z]*$  # non uppercase letters until the end
  |
    [^0-9]*$
  |
    [^!#$%^&*]*$
  |
    .*?job corps # any characters and "job corps"
)
[^@]* # characters that are not a @
$     # end of the string

demo

Note: you can write the range #$%& like #-& to win a character.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

stribizhev, your answer is correct

^(?!.job corps)(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[!#$%^&])(?!.@).$

can verify the expression in following url:

http://www.freeformatter.com/regex-tester.html

Community
  • 1
  • 1
Kapil
  • 1,823
  • 6
  • 25
  • 47