Regex matching digit and letters or only digits - regex

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,}/

Related

Regex to match only the following strings

I have a few strings and I need some help with constructing Regex to match them.
The example strings are:
AAPL10.XX1.XX2
AAA34CL
AAXL23.XLF2
AAPL
I have tried few expressions but couldn't achieve exact results. They are of the following:
[0-9A-Z]+\.?[0-9A-Z]$
[A-Z0-9]*\.?[^.]$
Following are some of the points which should be maintained:
The pattern should only contain capital letters and digits and no small letters are allowed.
The '.' in the middle of the text is optional. And the maximum number of times it can appear is only 2.
It should not have any special characters at the end.
Please ask me for any clarification.
You can write the pattern as:
^[A-Z\d]+(?:\.[A-Z\d]+){0,2}$
The pattern matches:
^ Start of string
[A-Z\d]+ Match 1+ chars A-Z or a digit
(?:\.[A-Z\d]+){0,2} Repeat 0 - 2 times a . and 1+ chars A-Z or a digit
$ End of string
Regex demo

Regex for combination of alphnumeric letters which has at least 2 uppercase letter or 1 number?

I need a regex for combination of numbers and uppercase letters and maybe lowercase letters and /,- characters, which contains at least 4 characters.
But of course it should contain at least 2 uppercase letter or one number.
I tried this:
barcode_regex = r"(?=(?:.+[A-Z]))(?=(?:.+[0-9]))([a-zA-Z0-9/-]{4,})"
For example match such cases as follows:
ametFXUT0
G197-6STK
adipiscXWWFHH
A654/9023847
HYJ/54GFJ
hgdy67h
You could use a single lookahead to assert at least 4 characters, and the match either a single digit or 2 uppercase chars in the allowed ranges.
^(?=.{4})(?:[A-Za-z/,-]*\d|(?:[a-z\d/,-]*[A-Z]){2})[A-Za-z\d/,-]*$
Explanation
^ Start of string
(?=.{4}) Assert 4 charcters
(?: Non capture group
[A-Za-z/,-]*\d Match optional allowed characters without a digit, then match a digit
| Or
(?:[a-z\d/,-]*[A-Z]){2} Match 2 times optional allowed characters withtout an uppercase char, then match an uppercase char
) Close non capture group
[A-Za-z\d/,-]* Match optional allowed characters
$ End of string
See a regex demo.
You could use two lookaheads combined via an alternation to check for 2 uppercase or 1 number:
^(?:(?=.*[A-Z].*[A-Z])|(?=.*\d))[A-Za-z0-9/-]+$
Demo
This regex patterns says to:
^
(?:
(?=.*[A-Z].*[A-Z]) assert that 2 or more uppercase are present
| OR
(?=.*\d) assert that at least one digit is present
)
[A-Za-z0-9/-]+ match any alphanumeric content (plus forward slash or dash)
$

How to sum characters and digits with 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

How do I write a regex for words having alphanumeric charcters but not made of only numbers?

For a line input "Abcd abcd1a 5ever qw3-fne superb5 1234 0"
I am trying to match words having letters and numbers, like "Abcd","abcd1a","5ever", "superb5","qw3","fne". But it should not match words having only numbers, like "1234", "0".
Words are separated by all the characters other than above alphanumerics.
I tried this regex (?![0-9])([A-Za-z0-9]+) which fails to match the word "5ever" but works properly for everything else.
How do I write this regex so that it also matches the word "5ever" in full?
Option 1 - Negative lookahead
See regex in use here
\b(?!\d+\b)[^\W_]+
\b(?!\d+\b)[A-Za-z\d]+
\b(?!\d+\b)[a-z\d]+ # With case-insensitive flag enabled
\b Assert position as a word boundary
(?!\d+\b) Negative lookahead ensuring the whole word isn't made up of only digits
[^\W_]+ or [A-Za-z\d]+ Matches only letters or digits one or more times
Option 2 - Without lookahead
Another alternative as seen in use here (case-insensitive i flag enabled):
\b\d*[a-z][a-z\d]* # With case-insensitive flag enabled
\b\d*[A-Za-z][A-Za-z\d]*
\b Assert position as a word boundary
\d* Match any digit any number of times
[a-z] Match any letter (with i flag enabled this also matches A-Z)
[a-z\d]* Match any letter or digit any number of times
Matches the following from the string Abcd abcd1a 5ever qw3-fne superb5 1234 0:
Abcd
abcd1a
5ever
qw3
fne
superb5
I came up with the following regex:
/\d*[a-z_]+\w*/ig
\d* starts with possible digit(s)
[a-z_]+ contains letter or underscore in qty one and more
\w* possibly followed by any characters after that letter
ig case insensitive and global flags
DEMO with detailed explanation

regular expression matches wrong String

I would like to match String that contains letters and numbers WITHOUT space
I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters
This is what it should do :
Nope
Nope 2
MatchPlease123
If you want to try in live:
http://rubular.com/r/pFMkk9ATc0
Thank you
You may use
/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/
See the regex demo (a bit modified since the input is a multiline string).
Details:
^ - start of a string
(?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
(?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
[a-zA-Z0-9]* - 0+ ASCII letters or digits
$ - end of string.
I think this simplest one will also be helpful
Regex demo
Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$
1. ^ start of string.
2. (?=.*\d) positive look ahead for digit.
3. (?=.*[a-zA-Z]) positive look alphabets.
4. [a-zA-Z\d]+ match all digits A-Z and a-z
5. $ end of string.