I am writing some regex to look for system passwords that might have been shared over chat.
I thought I had came up with a decent pattern but my RegEx is matching the rules anywhere in the line:
\b(?=[\S] )(?=(?:[a-zA-z]))(?=(?:. \d))(?=(?:. [$#]))(?!=[\s]*)([A-Za-z\d$#]{12,24})
I think I am using my boundaries improperly, what am I doing wrong?
this is RealPa$$word and it doesn't matter that the number is all the way here 1
this is RealPa$$word and it doesnt match because there is no number.
Thanks!
Update Here is a link to the Regexr i am using to validate.
CodePudding user response:
You have some superfluous assertions and groups. This (?=[\S] )
asserts a non whitespace char to the right, but the character class at the end of the pattern [A-Za-z\d$#]{12,24}
does not match a space so you can omit that one.
This assertion (?!=[\s]*)
is also unnecessary as there can also no =
being matched by the character class.
Also, for a match only, you don't need a capture group.
If you assert this right after the word boundary [a-zA-z]
(Note that is should be [a-zA-Z]
) then you can also match it.
If you don't want to match spaces, the .
should be more specific without matching spaces, like matching optional non digits without spaces and then matching a single digit (?=[^\d\s]*\d)
If you want to have a boundary at the right, the \b
would not work as there is $
and #
in the character class. You might assert a whitespace boundary at the right instead to not get partial matches for the [A-Za-z\d$#]{12,23}
part.
As you can match the first [a-zA-Z]
the quantifier can be {11,23}
instead of {12,24}
\b[a-zA-Z](?=[^\d\s]*\d)(?=[^\s$#]*[$#])[A-Za-z\d$#]{11,23}(?!\S)