Creating a regex that allows 9 or 10 digits - regex

I need help with a regex
I need it to match either a 9 or 10 digit value that starts with 50.
I have:
^[ ]*(50)[0-9]{7}[ ]*$
which allows 9 digits.
How can I expand this so that it also allows 10 digits?

Add the range {7,8}
^[ ]*(50)[0-9]{7,8}[ ]*$
FYI this site describes the standard quantifiers that you can use in a regular expression:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times

Try with following regex:
^[ ]*50\d{7,8}[ ]*$

This regex will match what you need:
^\s*50\d{7,8}\s*$
This will match all 9 or 10 digit numbers starting with 50 with an unlimited number of spaces before, or after, them on the line.
If you want to match all 9 or 10 digit numbers starting with 50 regardless of position and number of spaces etc then:
50\d{7,8}
will do exactly what you need.

Here's what you need: (50)\d{7,8}

Related

Regex match 2 letters and up to 10 digits afterwards

I have the following regex which is matching the first 2 letters RR and then 4 numbers after.
RR[0-9]{4}
How can I change it to detect the first 2 letters RR and then up to 10 digits afterwards?
I know I can do...
^[0-9]*$
To match all numbers but how can I limit this and add it to the first regex?
You can use RR\d{0,10}. This matches RR, followed by 0 to 10 digits, i.e. up to 10 digits.

Regular Expression that needs to match three exact digits in a four digit number

The regular expression that I am trying to create should match all numbers that contain three '8's in any 4 digit number. The regular expression that I have only matches the first 10 numbers out of the list of 15 numbers. Any suggestions will be greatly appreciated.
\b[0-9]*(?:8[0-9]*[0-9]?8|8[0-9]*[0-9]?8|8[0-9]*[0-9]?8)\b
Test data:
8088 8188 8288 8388 8488 8808 8818 8828 8838 8848 8880 8881 8882 8883 8884
The last five numbers should also match, but don't.
You can use
\b(?=\d{4}\b)(?:[0-79]*8){3}[0-79]*\b
See the regex demo.
Details:
\b - a word boundary
(?=\d{4}\b) - there must be 4 digits immediately on the right and they should be followed with a word boundary
(?:[0-79]*8){3} - three occurrences of any 0 or more digits but 8 and then 8
[0-79]* - any 0 or more digits but 8
\b - word boundary.
If it's guaranteed that the number is a four-digit number, then you can try the following:
\b8*[0-79]8*\b
To analyze what each part matches, you can check using,
\b(8*)[0-79](8*)\b
This should do it. This will match any of the 4 patterns.
([\d888]|[8\d88]|[88\d8]|[888\d])
You may want to add a check for the delimiter (in your example the space) as this pattern will match across the spaces giving you many more results
\b(\d?8{3}\d?)\b
this makes the first and last digit in the word bound optional, use
either ? or {0,1}
add quantifier to your eight to have exactly
number of eights you need {3}
replace [0-9] with \d as
Digit for brewity
supposed you have only numbers of length 4. Otherwise use an alternative without optional digits: \b(\d8{3}|8{3}\d)\b

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]+$

6 digits regular expression

I need a regular expression that requires at least ONE digits and SIX maximum.
I've worked out this, but neither of them seems to work.
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
^[0-999999]$
Any other suggestion?
You can use range quantifier {min,max} to specify minimum of 1 digit and maximum of 6 digits as:
^[0-9]{1,6}$
Explanation:
^ : Start anchor
[0-9] : Character class to match one of the 10 digits
{1,6} : Range quantifier. Minimum 1 repetition and maximum 6.
$ : End anchor
Why did your regex not work ?
You were almost close on the regex:
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
Since you had escaped the ? by preceding it with the \, the ? was no more acting as a regex meta-character ( for 0 or 1 repetitions) but was being treated literally.
To fix it just remove the \ and you are there.
See it on rubular.
The quantifier based regex is shorter, more readable and can easily be extended to any number of digits.
Your second regex:
^[0-999999]$
is equivalent to:
^[0-9]$
which matches strings with exactly one digit. They are equivalent because a character class [aaaab] is same as [ab].
^\d{1,6}$
....................
You could try
^[0-9]{1,6}$
it should work.
^[0-9]{1,6}$ should do it. I don't know VB.NET good enough to know if it's the same there.
For examples, have a look at the Wikipedia.
\b\d{1,6}\b
Explanation
\b # word boundary - start
\d # any digits between 0 to 9 (inclusive)
{1,6} # length - min 1 digit or max 6 digits
\b # word boundary - end
^[a-zA-Z0-9]{1,6}$
regex 6 digit number and alphabet in angular
/^[0-9][0-9][0-9][0-9]$/
Enter 4 digit number only