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
Related
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.
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
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]+$
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}
I need a regex to match a series of one or more n-digit numbers, separated by comma, ie:
abc12345def returns 12345
abc12345,23456def returns 12345,23456
so far I got this: \d{5}(,\d{5})*
problem is it also matches in cases like these:
123456 returns 12345, but I need it not to match if the number is longer than 5. So I need numbers of exactly 5 digits, and if a number is shorter or longer it's a no-match
Thanks
Which language are you using for your regexes? You want to put non-digit markers around your \d{5}'s; here is the Perl syntax (with a negative look-ahead/look-behind fix by Lukasz):
(?<![\d,])\d{5}(,\d{5})*(?![\d,])
Actually I think I got it! (?<!\d)\d{5}(?!\d)(,(?<!\d)\d{5}(?!\d))*
I used the look-ahead and look-behind
Thanks.
You could use this one:
/\D?\d{5}(?:,\d{5})?\D?/
explanation:
/ : regex delimiter
\D? : non digit optionnal
\d{5} : 5 digits
(?: : begining of non-capture group
,\d{5} : comma and 5 digits
)? : end of group optionnal
\D? : non digit optionnal
/ : regex delimiter