I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:
d4rt
df5h
34d6
4567
But not 'erty'.
I get that pattern ([a-z]+|[0-9]+){4} but that keeps me the 4 letters case.
Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*\d)[a-z0-9]{4}$
Explanation:
^ --> Start of input
(?=.*\d) --> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set
$ --> End of input
Demo
Related
I have to check the validation of a password that must have at least 3 capital letters, 3 lower case letters, 2 digits, at least one of those characters (!##$*), and the trickiest one it can not have the same character in a row. For example, "beer" is not allowed.
That's what I have done but it doesn't do a lot:
(?=[0-9]{2})&(?=[a-z]{3})&(?=[A-Z]{3})&(?=[!##$*])&(?:(?!.([a-z]|[0-9]|[A-Z]|[!##$*]{2})))
You may use the following pattern:
^(?=(?:.*[A-Z]){3})(?=(?:.*[a-z]){3})(?=(?:.*[0-9]){2})(?=.*[!##$*])(?!.*(.)\1).*$
Demo.
Breakdown:
^ - Beginning of string.
(?=(?:.*[A-Z]){3}) - A positive Lookahead to assert at least 3 capital letters.
(?=(?:.*[a-z]){3}) - A positive Lookahead to assert at least 3 lowercase letters.
(?=(?:.*[0-9]){2}) - A positive Lookahead to assert at least 2 digits.
(?=.*[!##$*]) - A positive Lookahead to assert at least one symbol.
(?!.*(.)\1) - A negative Lookahead to reject the same character repeated twice in a row.
.*$ - Match zero or more characters until the end of the string.
Note: If you want to prevent the user from using any additional characters, you may replace the final .* part with:
[A-Za-z0-9!##$*]*
I need to select rows from postgres where a part number has a three digit number (300-399) near the beginning. I just need a match/no match.
I'm mostly there with:
WHERE name ~ '^[A-Z]{0,5}3[0-9]{2}[^0-9]?*'
The part numbers
can be just 3 digits long,
can have up to 5 characters before the number
can have characters after the number
must exclude 4 digit numbers
These should match:
323
A335
AB359-B2
BB311BB
These should not match:
3234
A3357
AB3590-B
With the above, the 'should match' pass, but the 'should not match' are also passing. It seems the question mark that checks for a non-digit ([^0-9]?) allows the digits through, but I can't make it required since a simple 3-digit part number would not match.
Thanks!
This regexp passes all your tests.
'^[^\d]{0,5}\d{1,3}(\y|[^\d])'
The first caret ^ anchors to the start.
The [^\d]{0,5} allows up to five non-digit characters.
The \d{1,3} allows one to three each digit characters.
The (\y|[^\d]) alternation matches either a non-digit character or a word boundary such as the end of the string.
The pattern you tried could possibly also match the first 3 digits in a string with 4 digits as the character class at the end is optional [^0-9]?
If you don't make it optional [^0-9], it would not match a only 323 as there is a character expected after it which is any char except a digit.
If there can be characters after the number, but not a digit, you can use a negative lookahead (?!\d) to assert not a digit directly to the right.
^[A-Z]{0,5}3[0-9]{2}(?!\d)
Explanation
^ Start of string
[A-Z]{0,5} Match 0-5 times a char A-Z
3[0-9]{2} Match 3 and 2 digits
(?!\d) Negative lookahead, assert what is directly to the right is not a digit
Regex demo | Postgresql demo
I am searching for a regex which cover this usecases:
The length of the string is 8
The first two characters "can" contain 1 digit, the rest of the 6 Charatcers are digits
Two digits at the beginning are not allowed
Examples:
AB123456 --> good
1A789563 --> good
A2547896 --> good
11111111 --> BAD
I tried with:
/^[a-zA-Z]{2}\d{6}$/
But this allow two digits at the beginning.
Thank you for your help.
You may use a "spelled-out" regex approach where you list all possible variations in a grouping construct with alternation operators:
^(?:[a-zA-Z][0-9]|[0-9][a-zA-Z]|[a-zA-Z]{2})[0-9]{6}$
Or, if your regex engine supports lookaheads
^(?![0-9]{2})[0-9a-zA-Z]{2}[0-9]{6}$
See the first regex demo and the second regex demo.
The ^ asserts the position at the start of the string, then the (?:[a-zA-Z][0-9]|[0-9][a-zA-Z]|[a-zA-Z]{2}) non-capturing group matches a letter + digit, digit + letter or just two letters. Then, [0-9]{6} matches 6 digits up to the end of the string ($).
The second regex matches the start of a string (^), then fails the match if the first two chars are digits ((?![0-9]{2})), then matches two alphnumeric chars ([0-9A-Za-z]{2}) and then six digits ([0-9]{6}), and asserts the position at the end of the string ($).
I am trying to do a regex to get this cases:
Correct:
IUG4455
I4UG455
A4U345A
Wrong:
IUGG453
IIUG44555
need to be exactly 4 letters (in any order) and exactly 3 digits (in any order).
i tried use that expression
[A-Z]{3}\\d{4}
but it only accept start with letters (4) then digits (3).
You have a couple of options for this:
Option 1: See regex in use here
\b(?=(?:\d*[A-Z]){3})(?=(?:[A-Z]*\d){4})[A-Z\d]{7}\b
\b Assert position as a word boundary
(?=(?:\d*[A-Z]){3}) Positive lookahead ensuring the following matches
(?:\d*[A-Z]){3} Match the following exactly 3 times
\d* Match any digit any number of times
[A-Z] Match any uppercase ASCII character
(?=(?:[A-Z]*\d){4}) Positive lookahead ensuring the following matches
(?:[A-Z]*\d){4} Match the following exactly 4 times
[A-Z]* Match any uppercase ASCII character any number of times
\d Match any digit
[A-Z\d]{7} Match any digit or uppercase ASCII character exactly 7 times
\b Assert position as a word boundary
If speed needs to be taken into consideration, you can expand the above option and use the following:
\b(?=\d*[A-Z]\d*[A-Z]\d*[A-Z])(?=[A-Z]*\d[A-Z]*\d[A-Z]*\d[A-Z]*\d)[A-Z\d]{7}\b
Option 2: See regex in use here
\b(?=(?:\d*[A-Z]){3}(?!\d*[A-Z]))(?=(?:[A-Z]*\d){4}(?![A-Z]*\d))[A-Z\d]+\b
Similar to Option 1, but uses negative lookahead to ensure an extra character (uppercase ASCII letter or digit) doesn't exist in the string.
Having two positive lookaheads back-to-back simulates an and such that it ensures both subpatterns are satisfied starting at that particular position. Since you have two conditions (3 uppercase ASCII letters and 4 digits), you should use two lookaheads.
As an alternative,
(?:(?<d>\d)|(?<c>[A-Z])){7}(?<-d>){3}(?<-c>){4}
doesn't require any lookarounds. It just matches seven letter-or-digits and then checks it found 3 digits and 4 letters.
Adjust the 3 and 4 to taste... your examples have 4 digits and 3 letters.
Also add word boundaries or anchors depending on whether you are trying to match whole words or a whole string.
I need help using regular expression. I have some strings with these possibility use cases. The string will always start with a capital letters followed by 3 numbers then with a hyphen, followed by numbers:
A012-123
B001-012
C023-456
I've tried: [A-Z0-9]-[0-9] and i can't get a match. Can someone show me how to construct this correctly?
[A-Z][0-9]{3}-[0-9]{3}
The {3} means that, match only three times. This will match any string which starts with a capital letter, followed by 3 digits, a hypen and 3 digits.
But if the number of digits after - can be anything, then you can use
[A-Z][0-9]{3}-[0-9]+
This match any string which starts with a capital letter, followed by 3 digits, a hypen and one or more digits.
Note: Instead of writing [0-9], you can use \d. They both are one and the same. So your first regex will become
[A-Z]\d{3}-\d{3}