Regular expression 2 digits 10 letters/digits - regex

I'm trying to make a regular expression, but something isn't working for me, the requirements are the following:
Min length is 1
Max length is 12
The first 2 symbols must be numbers
Next 10 must be either letters or numbers
This is what I have so far
/^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$/
Can you guys tell me what I'm doing wrong?

Your pattern ^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$ matches 0, 1 or 2 digits at the start.
Then it matches 2 chars [a-z][A-Z] being a lowercase and an uppercase char A-Z which should be present in the string, and also makes the string length at least 2 chars.
You can make the second digit optional, and use 1 character class for the letters or numbers.
The length then has a minumum of 1, and a maximum of 12.
^(?!\d[a-zA-Z])\d\d?[a-zA-Z0-9]{0,10}$
^ Start of string
(?!\d[a-zA-Z]) negative lookahead, assert not a digit followed by a-zA-Z
\d\d? Match 1 or 2 digits
[a-zA-Z0-9]{0,10} Match 0-10 repetitions of any of the listed ranges
$ End of string
Regex demo
Or a version withtout a lookahead as suggested by #Scratte in the comments, matching a single digit and optionally a second digit followed by 0-10 repetitions of the listed ranges:
^\d(?:\d[A-Za-z\d]{0,10})?$
Regex demo

Related

Regex to match a string containing 14 digits and 1 character at any position

I need a regular expression that matches a string of 15 characters where 14 of them are digits and 1 is a character. The character can be in any position of the string.
I have the following long regex:
^.\d{14}|\d{1}.\d{13}|\d{2}.\d{12}|\d{3}.\d{11}|\d{4}.\d{10}|\d{5}.\d{9}|\d{6}.\d{8}|\d{7}.\d{7}|\d{8}.\d{6}|\d{9}.\d{5}|\d{10}.\d{4}|\d{11}.\d{3}|\d{12}.\d{2}|\d{13}.\d{1}|\d{14}.$
Can it be simplified?
Here is a sample match: 1000-1234567890
(?=^.{15}$)\d{0,14}\D\d{0,14}$
First check the string is 15 characters long, then has 0-14 digits, one non-digit, then 0-14 digits.
This isn't exactly the same as the original regex, which allows 15 digits in a row. To get that, simply change \D to .
We can use a lookaround trick here:
^(?!(?:.*\D){2})(?=.{15}$)\d*\D\d*$
This regex pattern says to match:
^ from the start of the string
(?!(?:.*\D){2}) assert that 2 or more non digits do NOT occur (implying at most 1 occurs)
(?=.{15}$) assert length is 15 characaters
\d*\D\d* then a non digit surrounded, possibly, on either side by numbers
$ end of the string

Regular expression to validate the given input which accepts one space or Hyphen which is part of the text length it has minimum and maximum length

The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9]+[ -]?[A-Z0-9]+)(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
There is no need to repeat range quantifier {5,10} multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1)+$)[A-Z0-9]*[ -]?[A-Z0-9]+$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{5,10}$): Assert that we have 5 to 10 chars till end
([A-Z0-9]): Match a letter or digit and capture in group #1
(?!(?:[ -]?\1)+$): Negative lookahead to fail the match if same captured value is repeated till end
[A-Z0-9]*: Match 0 or more letter or digit
[ -]?: Match optional space or hyphen
[A-Z0-9]+: Match 1 or more letter or digit
$: End

Regex expression for numbers and leading zeros just with a dot and decimal

I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.

regex - test for exactly 1 number and exactly 2 letters

Please assist me with creating this regex.
A total/exactly 3 digit alphanumeric
Exactly 1 numeric excluding 0 and 1 2-9
Exactly 2 Alpha excluding letter O and L - o and l
Number can be in any position
Valid codes:
A2M
HH9
3AM
Invalid Codes
10M (too many digits and invalid digits,
22A (two many digits),
MAB (missing digit)
MA2M (too long, not length of 3)
thank you for all the help. Here is the regex I will use, this one removes the letter L and lowercase l:
/([2-9]{1}[A-KMNP-Za-kmnp-z]{2}|[A-KMNP-Za-kmnp-z]{1}[2-9]{1}[A-KMNP-Za-kmnp-z]{1}|[A-KMNP-Za-kmnp-z]{2}[2-9]{1})/g
You may be able to shorten your regex with use of a lookahead:
^(?=\D*\d\D*$)[A-KMNP-Za-kmnp-z2-9]{3}$
RegEx Demo
RegEx Details:
^: Start
(?=\D*\d\D*$): Lookahead to ensure that we have exactly one digit
[A-KMNP-Za-kmnp-z2-9]{3}: Match any letter or digit except [01LlOo] exactly 3 times
$: End

Match a string with a word and a digit 1-9

My regex is weak, in the case of the following string
"OtherId":47
"OtherId":7
"MyId":47 (Match this one)
"MyId":7
I want to pick up the string that has "MyId" and a number that is not 1 - 9
I thought I could just use:
RegEx: How can I match all numbers greater than 49?
Combined using:
Regular Expressions: Is there an AND operator?
But its not happening... you can see my failed attempt here:
https://www.regextester.com/index.php?fam=99753
Which is
\b"MyId":\b(?=.*^[0-10]\d)
What am I doing wrong?
You can use this regex to match any digit >= 10:
^"MyId":[1-9][0-9]+$
RegEx Demo
If leading zeroes are to be allowed as well then use:
^"MyId":0*[1-9][0-9]+$
[1-9] makes sure number starts with 1-9 and [0-9]+ match 1 or more any digits after first digit.
Essentially, you are looking for 2 or more digits:
\"MyId\"\:(\d{2,})
I have escaped the quotes and colon, and {2,} means 2 or more.
If you need exact match to any number greater than 9
^"MyId":[1-9][0-9]+$