Building a regex to match passwords stored in plain text.
8-15 characters, must contain at least:
1 uppercase letter [A-Z]
1 lowercase letter [a-z]
1 number \d
1 special character [!##\$%\^&\*]
The problem I have is when the password is inline with other text or spaces after, it doesn't return a match. When it's on its own without spaces it matches.
Example:
This is a Testing!23 surrounded by other text.
Testing!23
(?=.{8,15})(?=.*[!##\$%\^&\*])(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*
You want to find all non-whitespace chunks matching the conditions you outlined.
Use
(?<!\S)(?=\S{8,15}(?!\S))(?=[^!##$%^&*\s]*[!##$%^&*])(?=[^\s\d]*\d)(?=[^\sa-z]*[a-z])(?=[^\sA-Z]*[A-Z])\S+
See the regex demo
Details
(?<!\S) - a whitespace or start of string should be right before the current position
(?=\S{8,15}(?!\S)) - right after the current position, there must be 8 to 15 non-whitespace chars followed with either whitespace or end of string
(?=[^!##$%^&*\s]*[!##$%^&*]) - there must be a char from the [!##$%^&*] set after zero or more non-whitespace chars outside of the set
(?=[^\s\d]*\d) - there must be a digit after 0+ non-whitespace and non-digit chars
(?=[^\sa-z]*[a-z]) - 1 lowercase letter must appear after 0+ chars other than whitespace and lowercase letters
(?=[^\sA-Z]*[A-Z]) - 1 uppercase letter must appear after 0+ chars other than whitespace and uppercase letters
\S+ - all checks are over, and if they succeed, match and consume 1+ non-whitespace chars (finally).
Related
I am writing an expression to validate a username with the following requirements:
length between 6 and 20,
must start with a letter,
numbers are allowed,
dot is optional or only one is allowed.
must not end with a dot
Tried with but didn't work:
^(([a-zA-Z])(\.{0,1})([a-zA-Z0-9]*)){6,20}$
another option
^([a-zA-Z]+([\\.]?)+([a-zA-Z0-9]*)){6,20}$
You may use an expression that will match a letter at the start and the 5 to 19 letters, digits or dots that should only appear in the middle of the string.
You may use
^(?=.{5,19}$)(?=[A-Za-z])[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*$
See the regex demo.
Details
^ - start of string
(?=.{5,19}$) - a positive lookahead that matches a string that contains any five to nineteen characters other than line break chars
(?=[A-Za-z]) - the first char must be an ASCII letter
[A-Za-z0-9]+ - one or more letters or digits
(?:\.[A-Za-z0-9]+)* - 0 or more repetitions of
\. - a dot
[A-Za-z0-9]+ - one or more letters or digits
$ - end of string.
I am using the below regex expression to ensure string is max 50 characters in length and that each word starts with uppercase letter:
reMatch("Jet Black","^(?=.{0,50}$)(^|^([A-Z][a-z]* +)*([A-Z][a-z]* *)$)")
This works, but I would also like to allow for option to separate words with / character. Example: Jet/Black and Jet / Black with a space in between.
Your suggestions are highly appreciated! Mike.
If you do not care if there may be several spaces, or slahes or intermingled spaces and slashes you may use
^(?=.{0,50}$)(?:[A-Z][a-z]*(?:[ /]+[A-Z][a-z]*)*)?$
See the regex demo.
To only allow spaces and an optional single slash with (white)spaces after use
^(?=.{0,50}$)(?:[A-Z][a-z]*(?:\s*(?:/\s*)?[A-Z][a-z]*)*)?$
See this regex demo
Details
^ - start of string
(?=.{0,50}$) - string should contain only 0 to 50 chars other than linebreak chars (same as (?!.{51}))
(?:[A-Z][a-z]*(?:\s*(?:/\s*)?[A-Z][a-z]*)*)? - an optional sequence of
[A-Z][a-z]* - an uppercase ASCII letter and 0+ lowercase ASCII letters
(?:\s*(?:/\s*)?[A-Z][a-z]*)* - 0 or more sequences of
\s* - 0+ whitespaces
(?:/\s*)? - an optional / and 0+ whitespaces
[A-Z][a-z]* - an uppercase ASCII letter and 0+ lowercase ASCII letters
$ - end of string.
I'm working with an SAP application called information steward and creating a rule where names will have to be in title case (ie each word is capitalized).
I've formulated the following rule:
BEGIN
IF(match_regex($name, '(^(\b[A-Z]\w*\s*)+$)', null)) RETURN TRUE;
ELSE RETURN FALSE;
END
Although it is successful it appears to accept inputs which should be identified as 'FALSE'. Please see the attached screenshot.
'TesT Name' and 'TEST NAME' should be FALSE but are instead passing under this regex.
Any help/guidance with the regex would be very useful.
The (^(\b[A-Z]\w*\s*)+$) regex presents a pattern that matches a string that fully matches:
^ - start of string
(\b[A-Z]\w*\s*)+ - 1 or more occurrences (due to (...)+) of
\b - a word boundary
[A-Z] - an uppercase ASCII letter
\w* - 0 or more letters/digits/underscores
\s* - 0+ whitespaces
$ - end of string.
As you see, it allows trailing whitespace, and \w matches what [A-Za-z0-9_] matches, i.e. it matches both lower- and uppercase letters.
You want to only match lowercase letters after initial uppercase ones, also allowing - and _ chars. You may use
^[A-Z][a-z0-9_-]*(\s+[A-Z][a-z0-9_-]*)*$
See the regex demo.
Details
^ - start of string anchor
[A-Z][a-z0-9_-]* - an uppercase letter followed with 0+ lowercase letters, digits, _ or - chars
(\s+[A-Z][a-z0-9_-]*)* - zero or more occurrences of:
\s+ - 1 or more whitespaces
[A-Z][a-z0-9_-]* - an uppercase letter followed with 0+ lowercase letters, digits, _ or - chars
$ - end of string.
I would write your regex as:
^[A-Z]\w*(?:\s+[A-Z]\w*)*$
This says to match a single word starting with a capital letter, then followed by one or more spaces and another word starting with a capital, this quantity zero or more times.
I phrase a matching word as starting with [A-Z] followed by \w*, meaning zero or more word characters. This allows for things like A to match.
Demo
Edit:
Based on the comments above, if you want some other character class to represent what follows the initial uppercase letter, then do that instead:
^[A-Z][something]*(?:\s+[A-Z][something]*)*$
where [something] is your character class.
I want my regex to allow alphanumeric characters, "/_-" and white spaces in between but it must always have at least one alphanumeric character.
my validation goes like this,
/^([A-Za-z0-9/-]+[A-Za-z0-9/-\s]*[A-Za-z0-9/_-]+)$/
It should accept **ABC_1-2-3 but it must not allow 123 or -_/ alone
Can somebody help me please.
The below given regex will capture strings with alpha-numeric characters with optional white space, hyphen and underscore in it. Try it.
([*A-Za-z]+(\s+)?([\d\-_]+)?)
Your regex is almost right, you need to add 2 positive lookaheads at the start to require at least 1 letter and at least 1 digit:
/^(?=.*[a-z])(?=.*\d)[a-z0-9\/_-][a-z0-9\/_\s-]*[a-z0-9\/_-]$/i
See the regex demo (in the demo, \s is replaced with a space since the demo is multiline).
Details:
^ - start of string
(?=.*[a-z]) - after any 0+ chars other than line break chars, there must be at least 1 letter (replace .* with [^a-z]* for better performance)
(?=.*\d) - after any 0+ chars other than line break chars, there must be at least 1 digit (replace.with\D` for better performance)
[a-z0-9\/_-] - a letter, digit, /, _ or -
[a-z0-9\/_\s-]* - 0+ letters, digits, /, whitespaces, _ or -
[a-z0-9\/_-] - a letter, digit, /, _ or -
$ - end of string.
The i modifier makes the pattern case insensitive.
I am trying to validate a password with the following rules:
Must have at least eight characters.
Must contain ONLY letters and digits.
Must contain at least two digits.
So far I wrote this code:
[0-9a-zA-Z] (?=(.*\d){2}) {8,}
Im not sure why the passwords I enter returns invalid although it follows the rules.
Remember that spaces are meaningful in a regex pattern, so you require at least 8 spaces at the end. There are no anchors in the regex, so the length limitation might not work even if you write a correct pattern. So far, this will match an alphanumeric, a space that is followed with 2 occurrences of any 0+ chars followed with a digit, but since there is space with {8,} quantifier, this pattern will never match anything.
You need
^(?=.{8})[a-zA-Z]*(?:\d[a-zA-Z]*){2}[a-zA-Z0-9]*$
See the regex demo
^ - start of string
(?=.{8}) - at least 8 chars
[a-zA-Z]* - zero or more letters
(?:\d[a-zA-Z]*){2} - 2 sequences of:
\d - a digit (may be replaced with [0-9])
[a-zA-Z]* - zero or more letters
[a-zA-Z0-9]* - 0+ alphanumeric chars
$ - end of string.
Alternatively, you may use
^(?=(?:[a-zA-Z]*\d){2})[a-zA-Z0-9]{8,}$
See another regex demo
Here, (?=(?:[a-zA-Z]*\d){2}) will require at least 2 occurrences of 0+ letters followed with a digit in the string and [a-zA-Z0-9]{8,} will match 8 or more alphanumeric chars.