How to sum characters and digits with regex? - regex

I have a situation with a pattern that always has length = 6, stating with characters and finishings with numbers. I may have 2 to 5 characters, completing length to 6.
Examples:
ABCDE1
ABCD12
ABC123
AB1234
Today we use a Regex like this ([A-Z]{4}\d{2}|[A-Z]{3}\d{3}|[A-Z]{5}\d{1}) but it can grow, and make this solution turn into a monster.
There is a way where I can get how many characters I have to set how many digits I should have?
OBS: We always have characters and then digits, never mixed up.

You can assert 6 chars A-Z or digits. Then start the match with 2-5 chars A-Z and match 1+ digits.
^(?=[A-Z0-9]{6}$)[A-Z]{2,5}\d+$
^ Start of string
(?=[A-Z0-9]{6}$) Positive lookahead, assert 6 chars either A-Z or a digit till end of string
[A-Z]{2,5} Match 2-5 chars A-Z
\d+ Match 1+ digits
$ End of string
Regex demo

Related

Regex matching digit and letters or only digits

I'm stuck trying to code a regex which match with those conditions:
string with more than 9 digits
string with more than 9 digits and letters
I can't figure out how to write my regex saying to it: digit or digit and letters can match but not letters.
Those string should match:
12345678987654567
jhsjd4567hsqdgqsgh456786576567kj
9l8j9n9k0n9n8n
Those string should not match:
loremipsum
a1
12567
My regex so far: /(?:\w){9,}/
Thanks a lot :)
I am interpreting your requirements as: match a string of more than nine characters which contains either digits only or digits and letters only.
const tests = [
'12345678987654567',
'jhsjd4567hsqdgqsgh456786576567kj',
'9l8j9n9k0n9n8n',
'loremipsum',
'a1',
'12567',
];
for (const t of tests) {
console.log(t.padEnd(35) +
/^(?=.*\d)[a-z\d]{10,}$/i.test(t)
)
}
The positive lookahead (?=.*\d) ensures that there is at least one digit in the string.
Remove the i flag if you want to match only lower-case letters.
Using \w can match both letters and digits, so using (?:\w){9,}, which can be written as \w{9,} can also match only letters or only underscores.
Reading the requirements, you can match 9 or more times a letter or a digit and make sure that the string does not contain only letters using a negative lookahead if that is supported.
If you want to match more than 9, you can use {10,} as the quantifier.
^(?![a-zA-Z]+$)[a-zA-Z0-9]{9,}$
The pattern matches:
^ Start of string
(?![a-zA-Z]+$) Negative lookahead, assert not only characters a-z A-Z in the string
[a-zA-Z0-9]{9,} Match 9 or more times chars a-z A-Z or a digit
$ End of string
Regex demo
Or using word boundaries:
\b(?![a-zA-Z]+\b)[a-zA-Z0-9]{9,}\b
Regex demo
I believe it should work
/([a-z]*[0-9][a-z]*){9,}/

Regex (Has between 1 and 4 digits and as many characters as possible)

Hi trying to create a regex that ensures you have between 1 and 4 number of digits and also as many characters as possible
Here's what I have written so far ^([A-Za-z]+([0-9]){1,4}$)
This doesnt allow me to have characters after the digits
You might repeat the whole part 1-4 times and match optional trailing chars a-z
^(?:[A-Za-z]*[0-9]){1,4}[A-Za-z]*$
The pattern matches
^ Start of string
(?: Non capture group
[A-Za-z]*[0-9]){1,4} Repeat matching 1-4 times optionalchars a-z and a single digit
[A-Za-z]* Optionally repeat char A-Za-z
$ End of string
Regex demo

Regex to match string of limited length and if number digit are present it can't be more than 5 digit

im looking for regex that can match string with requirement below.
must be 5 to 15 characters
Alphanumeric, can accept fully alphabet, if numeric are present, it must not exceed 5 digit and it can be in anywhere in the string.
Example accepted input
helloworld
123helloworld56
1h2e3l4l5oworld
12345
if the numeric digit exceeded 5 it shall be rejected. Example rejected input:
123456
123hello4567
So far i have tried while looking online and done some tweaking, but none work as expected.
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
^(?=.*\d){0,5}.{0,15}$
I have stuck on this for some time now, any help are appreciated!
If there can not be more than 5 digits in total, that means you should not be able to match 6 digits.
You can use a negative lookahead to assert what is on the right can not match 6 digits.
^(?!(?:[^\d\r\n]*\d){6})[a-zA-Z0-9]{5,15}$
Explanation
^ Start of string
(?! Negative lookahead, assert what is at the right is not
(?:[^\d\r\n]*\d){6} Match 6 times any char except a newline or a digit, then match a digit
) Close lookahead
[a-zA-Z0-9]{5,15} Match 5-15 times any of the listed in the character class
$ End of string
Regex demo
Note that using [1-9] in a character class does not match the 0, and \d will
About the patterns in the question
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
Here, the lookahead will always be true as all the parts in it are optional. It could also match an empty string as the quantifier {0,15} starts at 0, which makes it optional.
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
The pattern asserts a string with 5-15 times any of the listed in the character class. But the matching starts with 1-15 times a char a-zA-Z followed by matching 0-5 times a digit at the end of the string.
^(?=.*\d){0,5}.{0,15}$
The pattern optionally asserts 0-5 digits which is always true as it is optional. Then it matches 0-15 times any char.

Regex match checksum with or without dashes

To match a dash-less checksum I can do something like:
\b[0-9a-z]{32}\b
However, I'm seeing some checksums that also have dashes, such as:
d3bd55bf-062f-473b-9417-935f62c4c98a
While this is probably a fixed size, 8, then 4, then 4, then 4, then 12, I was wondering if I could do a regex where the number of non-dash digits adds up to 32. I think the answer is no, but hopefully some regex wizard can come up with something.
Here is a starting point for some sample inputs: https://regex101.com/r/K0IMKe/1.
You can use
\b[0-9a-z](?:-?[0-9a-z]){31}\b
See the regex demo.
It matches
\b - a word boundary
[0-9a-z] - a digit or a lowercase ASCII letter
(?:-?[0-9a-z]){31} - thirty-one repetitions of an optional - followed with a single digit or a lowercase ASCII letter
\b - a word boundary.
If you do not mind having a trailing - if there is a word char after it, at the end of a match, you may also use
\b(?:[0-9a-z]-?){32}\b
See this regex demo. Here, (?:[0-9a-z]-?){32} will match thirty-two repetitions of a digit or lowercase ASCII letter followed with an optional hyphen.
If there can be multiple dashes, you can assert 32 to 36 chars using a positive lookahead.
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+)*$
^ Start of string
(?=[a-z0-9-]{32,36}$) Positive lookahead, assert what is at the right is 32 - 36 repetitions of the listed characters
[a-z0-9]+ Match 1+ times any of the listed
(?: Non capture group
-[a-z0-9]+ Match a - followed by 1+ times any of the listed (the string can not end with a hyphen)
)* Close the group and match 0+ times to also match the string without dashes
$ End of string
Regex demo
If you want to limit the amount of dashes to 0 -4 times, you can change the quantifier * to {0,4}+
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+){0,4}+$
Regex demo

Regex, must contain a number, 1-5 chars and letters and digits

Specs:
1 - 5 chars length
Contains at least 1 number char
Only A-Z, a-z and 0-9 allowed
This is what I have sofar: ^[a-zA-Z0-9]{0,5}$
Problem I have this accepts 'AV' for example. It must contain a number to be valid.
https://regex101.com/r/i5VnXt/1
You need to add a positive lookahead (?=\D*\d) and use {1,5} limiting quantifier at the end to match 1 to 5 chars:
^(?=\D*\d)[a-zA-Z0-9]{1,5}$
See the regex demo
Details
^ - start of a string
(?=\D*\d) - a positive lookahead requiring a digit after 0 or more non-digit symbols
[a-zA-Z0-9]{1,5} - 1 to 5 (due to {1,5} limiting quantifier) consecutive alphanumeric chars (ASCII letters or digits)
$ - end of string.