Python regex that will require at least 6 characters to return true
Question:
one of the criteria is to accept strings that are at least 6 characters
and this is my code:
regex = ("^(?=.*[a-z]S)(?=.*[A-Z]S)(?=.*[0-9]S)")
i tried putting a {6,}
at the end of my code but it still accepts strings below 6 characters…
all of the requirements is:
- contains a lowercase letter (at least one)
- contains an uppercase letter (at least one)
- contains a digit (at least one)
- only contains alphanumeric characters (note that ‘_’ is not alphanumeric)
TEST CASES
test.assert_equals(bool(search(regex, 'fjd3IR9')), True)
test.assert_equals(bool(search(regex, 'ghdfj32')), False)
test.assert_equals(bool(search(regex, 'DSJKHD23')), False)
test.assert_equals(bool(search(regex, 'dsF43')), False)
test.assert_equals(bool(search(regex, '4fdg5Fj3')), True)
test.assert_equals(bool(search(regex, 'DHSJdhjsU')), False)
test.assert_equals(bool(search(regex, 'fjd3IR9.;')), False)
test.assert_equals(bool(search(regex, 'fjd3 IR9')), False)
test.assert_equals(bool(search(regex, 'djI38D55')), True)
test.assert_equals(bool(search(regex, 'a2.d412')), False)
test.assert_equals(bool(search(regex, 'JHD5FJ53')), False)
test.assert_equals(bool(search(regex, '!fdjn345')), False)
test.assert_equals(bool(search(regex, 'jfkdfj3j')), False)
test.assert_equals(bool(search(regex, '123')), False)
test.assert_equals(bool(search(regex, 'abc')), False)
test.assert_equals(bool(search(regex, '123abcABC')), True)
test.assert_equals(bool(search(regex, 'ABC123abc')), True)
test.assert_equals(bool(search(regex, 'Password123')), True)
all credits to codewars and to its author…
Answers:
Try this:
regex = (r"^[a-zA-Z0-9_s-]{6,}$")
If you use re module, it will find you any string with 6 or more chars if you test it.
Code here for ex:
import re
txt = "string"
print(re.search(r"^[a-zA-Z0-9_s-]{6,}$", txt))
If the strings has less than 6 chars, it will not be found.
one of the criteria is to accept strings that are at least 6 characters
and this is my code:
regex = ("^(?=.*[a-z]S)(?=.*[A-Z]S)(?=.*[0-9]S)")
i tried putting a {6,}
at the end of my code but it still accepts strings below 6 characters…
all of the requirements is:
- contains a lowercase letter (at least one)
- contains an uppercase letter (at least one)
- contains a digit (at least one)
- only contains alphanumeric characters (note that ‘_’ is not alphanumeric)
TEST CASES
test.assert_equals(bool(search(regex, 'fjd3IR9')), True)
test.assert_equals(bool(search(regex, 'ghdfj32')), False)
test.assert_equals(bool(search(regex, 'DSJKHD23')), False)
test.assert_equals(bool(search(regex, 'dsF43')), False)
test.assert_equals(bool(search(regex, '4fdg5Fj3')), True)
test.assert_equals(bool(search(regex, 'DHSJdhjsU')), False)
test.assert_equals(bool(search(regex, 'fjd3IR9.;')), False)
test.assert_equals(bool(search(regex, 'fjd3 IR9')), False)
test.assert_equals(bool(search(regex, 'djI38D55')), True)
test.assert_equals(bool(search(regex, 'a2.d412')), False)
test.assert_equals(bool(search(regex, 'JHD5FJ53')), False)
test.assert_equals(bool(search(regex, '!fdjn345')), False)
test.assert_equals(bool(search(regex, 'jfkdfj3j')), False)
test.assert_equals(bool(search(regex, '123')), False)
test.assert_equals(bool(search(regex, 'abc')), False)
test.assert_equals(bool(search(regex, '123abcABC')), True)
test.assert_equals(bool(search(regex, 'ABC123abc')), True)
test.assert_equals(bool(search(regex, 'Password123')), True)
all credits to codewars and to its author…
Try this:
regex = (r"^[a-zA-Z0-9_s-]{6,}$")
If you use re module, it will find you any string with 6 or more chars if you test it.
Code here for ex:
import re
txt = "string"
print(re.search(r"^[a-zA-Z0-9_s-]{6,}$", txt))
If the strings has less than 6 chars, it will not be found.