I have a workaround for this, but was hoping to find a purely regex solution.
The requirements are:
has one required character
only pulls from a pool of approved characters
minimum length of 4
single word, no whitespace
e.g.
required character: m
pool of characters: [a,b,e,l]
Possible matches:
mabel
abemal
labeam
won't match:
a mael
ama
label
So far I have this expression, but putting a {4,} after it thinks I'm talking about multiplying word matches by 4.
^\b(?:[abel]*[m]+[abel]*)\b
You can use
^(?=.*[m])[abelm]{4,}$
^ start of a line or string
Positive Lookahead (?=.*[m])
Asserts that the string contains at least 1 m character
[abelm]{4,} matches characters in the list abelm
between 4 and unlimited times, as many times as possible.
(greedy) (case sensitive)
$ end of a line or string
Related
I was wondering if it is possible to simplify the following regular expression
^(?!.*([,\._\- ]).*\1)(?!.*[',\._\- ]{2})(?!.*(['])(?:.*\2){2})[',\._\- \p{L}]+$
Regex Demo
Constraints
All of these characters can appear a maximum of one time each: _-.
The last character is a white space.
This character can appear a maximum of two times each: '
All special characters here can not appear consecutively.
Details
(?!.*([,\._\- ]).*\1) - There cannot be 2 or more occurrences of any one character in _-.
(?!.*[',\._\- ]{2}) - No char in the special char group can appear consecutively.
(?!.*(['])(?:.*\2){2}) - Single quote special char cannot appear 3 or more times.
You might write the pattern using a single capture group in combination with negated character classes making use of contrast
You don' have to create a capture group ([']) with a backreference \2 to repeat a single quote 3 times, but you can just repeat that char 3 times.
As there do not seem to be ' at the start or at the end, you can use a repeating pattern \p{L}+(?:[',._ -]\p{L}+)* to not have consecutive chars that you don't want.
Note that you don't have to escape the ' and _ in the character class, and you can move the - to the end so that you don't have to escape it.
^(?![^,._ \n-]*([,._ -]).*\1)(?!(?:[^'\n]*'){3})\p{L}+(?:[',._ -]\p{L}+)*$
Explanation
^ Start of string
(?![^,._ \n-]*([,._ -]).*\1) Assert not 2 of the same chars [,._ -]
(?!(?:[^'\n]*'){3}) Assert not 3 times '
\p{L}+ Match 1+ times any kind of letter
(?:[',._ -]\p{L}+)* Optionally repeat one of [',._ -] and again 1+ times any kind of letter
$ End of string
Regex demo
I wanted to create regex expression that only matches when any string has three or more character and if any + sign in the string then after and before + sign it must be minimum three characters required,
I have created one regex it fulfills me all requirement except one that before first + sign must be minimum three characters but it matches with less character
this is my current regex: (\+[a-z0-9]{3}|[a-z0-9]{0,3})$
ab+abx this string should not match but it matched in my regex
Example:
Valid Strings:
sss
sdfsgdf
4534534
dfs34543
sdafds+3232+sfdsafd
qwe+sdf
234+567
cvb+243
Invalid Strings:
a
aa
a+
aa+
+aa
+a
a+a
aa+aa
aaa+a
You can use this regex,
^[^+\n]{3,}(?:\+[^+\n]{3,})*$
Explanation:
^ - Start of string
[^+\n]{3,} - This ensures it matches any characters except + and newline, \n you can actually remove if the input you're trying to match doesn't contain any newlines and {3,} allows it to match at least three and more characters
(?:\+[^+\n]{3,})* - This part further allows matching of a + character then further separated by at least three or more characters and whole of it zero or more times to keep appearance of + character optional
$ - End of input
Demo
Edit: Updating solution where a space does not participate in counting the number of characters in either side of + where minimum number of character required were three
You can use this regex to ignore counting spaces within the text,
^(?:[^+\n ] *){3,}(?:\+ *(?:[^+\n ] *){3,})*$
Demo
Also, in case you're dealing with only alphanumeric text, you can use this simpler and easier to maintain regex,
^(?:[a-z0-9] *){3,}(?:\+ *(?:[a-z0-9] *){3,})*$
Demo
You could repeat 0+ times matching 3 or more times what is listed in the character class [a-z0-9] preceded by a plus sign:
^[a-z0-9]{3,}(?:\+[a-z0-9]{3,})*$
That will match:
^ Start of string
[a-z0-9]{3,} Match 3+ times what is listed in the character class
(?: Non capturing group
\+[a-z0-9]{3,} Match + sign followed by matching 3+ times what is listed in the character class
)* Close group and repeat 0+ times
$ End of string
It's actually simple to do, but I'm stucked in this solution.
I have a list of random characters with a length of 20 contains only capital characters and numbers. As example.
NC6DGL2L41ADTXEP20UP
F3KB7UXUBD5089BKANOY
A5P3UI57KW18UNF89AKL
6O36RJHDLNXW8Y1O1GBC
6CVAT6LTAHEKDRCB9KNH
K20L4MQRA5C677P2NNV8
726WYBOO0X7UTFMSN6VT
AYBECMW9AVJX9AX5F1ZZ
HWKWU0BEIWLHZZJYKDC1
TXLF9FYNIVZ7SHR92ZIH
My goal is to choose only these who doesn't contain a double character in an order like this.
F3KB7UXUBD5089BKANOY
I don't want strings like this, because there is a N character in an order.
NC6NNNN41ADTXEP20UP
(?!^.*([A-Z0-9])\1.*$)^[A-Z0-9]+$
See the demo
Negative Lookahead to make sure that 2 of the same characters do not sit together
(Edited to increase performance, see the other version through the demo link, v1 of the regex).
Breakdown of the regex:
(?! - start of the negative lookahead
^ - from the start of the string
.* - any character, any amount of times
([A-Z0-9]) - capture a character in the ranges given
\1 - the same characters as the first capture group
.*$ any character, any amount of times until the end of the string
) close negative lookahead
This section therefore means, outside of this, do not match anything that from start to finish contains 2 of the same character (in the ranges A-Z and 0-9) sitting together.
^ - from the start of the string
[A-Z0-9]+ - a character in the ranges given, one or more times
$ - until the end
This validation works well for allowing alphanumeric chars, spaces, and a dash, but I have not been able to set the max length to 23.
Regex:
(^\w+\s*(-?)(\s*\w+\s*)(\w+)$){0,23}
The cases I need to pass:
Winston1-Salem6
Winston-Salem
Winston Salem
1-two3
word2 with space
Cases I need to fail:
-Newberty-Los-
12345678901234567890123444
It may be more convenient to check the length separately, but you can use a lookahead to confirm that the entire expression is between 0 and 23 characters.
(?=^.{0,23}$)(^\w+\s*(-?)(\s*\w+\s*)(\w+)$)
http://rubular.com/r/GVIbG8hDKz
Just use a look ahead to assert a max length:
(?=^.{1,23}$)^\w+\s*(-?)(\s*\w+\s*)(\w+)$
Demo
Or a negative lookahead works too:
(?!^.{24,})^\w+\s*(-?)(\s*\w+\s*)(\w+)$
Demo
Variable width lookaheads are supported in most modern regex flavors
^(?!(^-|-$|.{24,})).*
Winston1-Salem6 - PASS
Winston-Salem - PASS
Winston Salem - PASS
1-two3 - PASS
word2 with space - PASS
-Newberty-Los- - FAIL
12345678901234567890123444 - FAIL
Demo
https://regex101.com/r/eM3qR9/2
Regex Explanation:
^(?!(^-|-$|.{24,})).*
Assert position at the beginning of the string «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!(^-|-$|.{24,}))»
Match the regex below and capture its match into backreference number 1 «(^-|-$|.{24,})»
Match this alternative «^-»
Assert position at the beginning of the string «^»
Match the character “-” literally «-»
Or match this alternative «-$»
Match the character “-” literally «-»
Assert position at the end of the string, or before the line break at the end of the string, if any «$»
Or match this alternative «.{24,}»
Match any single character that is NOT a line break character «.{23,}»
Between 24 and unlimited times, as many times as possible, giving back as needed (greedy) «{24,}»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Regex can't total lengths of matches up like you want.
Use
/^(\w+([ -]\w+)*)$/
and check the length of the group #1 manually after the match.
Edit: Thanks for the advice to make my question clearer :)
The Match is looking for 3 consecutive characters:
Regex Match =AaA653219
Regex Match = AA5556219
The code is ASP.NET 4.0. Here is the whole function:
public ValidationResult ApplyValidationRules()
{
ValidationResult result = new ValidationResult();
Regex regEx = new Regex(#"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");
bool valid = regEx.IsMatch(_Password);
if (!valid)
result.Errors.Add("Passwords must be 8-20 characters in length, contain at least one alpha character and one numeric character");
return result;
}
I've tried for over 3 hours to make this work, referencing the below with no luck =/
How can I find repeated characters with a regex in Java?
.net Regex for more than 2 consecutive letters
I have started with this for 8-20 characters a-Z 0-9 :
^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$
As Regex regEx = new Regex(#"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");
I've tried adding variations of the below with no luck:
/(.)\1{9,}/
.*([0-9A-Za-z])\\1+.*
((\\w)\\2+)+".
Any help would be much appreciated!
http://regexr.com?34vo9
The regular expression:
^(?=.{8,20}$)(([a-z0-9])\2?(?!\2))+$
The first lookahead ((?=.{8,20}$)) checks the length of your string. The second portion does your double character and validity checking by:
(
([a-z0-9]) Matching a character and storing it in a back reference.
\2? Optionally match one more EXACT COPY of that character.
(?!\2) Make sure the upcoming character is NOT the same character.
)+ Do this ad nauseum.
$ End of string.
Okay. I see you've added some additional requirements. My basic forumla still works, but we have to give you more of a step by step approach. SO:
^...$
Your whole regular expression will be dropped into start and end characters, for obvious reasons.
(?=.{n,m}$)
Length checking. Put this at the beginning of your regular expression with n as your minimum length and m as your maximum length.
(?=(?:[^REQ]*[REQ]){n,m})
Required characters. Place this at the beginning of your regular expression with REQ as your required character to require N to M of your character. YOu may drop the (?: ..){n,m} to require just one of that character.
(?:([VALID])\1?(?!\1))+
The rest of your expression. Replace VALID with your valid Characters. So, your Password Regex is:
^(?=.{8,20}$)(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])(?:([\w\d*?!:;])\1?(?!\1))+$
'Splained:
^
(?=.{8,20}$) 8 to 20 characters
(?=[^A-Za-z]*[A-Za-z]) At least one Alpha
(?=[^0-9]*[0-9]) At least one Numeric
(?:([\w\d*?!:;])\1?(?!\1))+ Valid Characters, not repeated thrice.
$
http://regexr.com?34vol Here's the new one in action.
Tightened up matching criteria as it was too broad; for example, "not A-Za-z" matches a lot more than is intended. The previous REGEX was matching on the string "ThiIsNot". For the most part, passwords are only going to contain alphanumeric and punctation characters, so I limited the scope, which made all matches more accurate. Used character classes for human readability. Added and exclusion list, and differentiated upper and lower case letters.
^(?=.{8,20}$)(?!(?:.*[01IiLlOo]))(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1})(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1})(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1})(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$
The breakdown:
^(?=.{8,20}$) - Positive lookahead that the string is between 8 and 20 chars
(?!(?:.*[01IiLlOo])) - Negative lookahead for any blacklisted chars
(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2}) - Verify that at least 2 alpha chars exist
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1}) - Verify that at least 1 lowercase alpha exists
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1}) - Verify that at least 1 uppercase alpha exists
(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1}) - Verify that at least 1 digit exists
(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1}) - Verify that at least 1 special/punctuation char exists
(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$ - Verify that no char is repeated more than twice in a row