I want to write a regular expression that must take a valid only the numerical values with 0, 1 or 2 digits after the decimal point.
So I tried to do it like: "^\\d+(\\.\\d){0,2}$" but it returns true even for numbers with 3 digits after the decimal point.
Any ideas what's wrong?
Your regex ^\d+(\.\d){0,2}$ matches 1 or 1.0 but also 1.0.0 because you specifiy a quantifier for 0-2 times for the group (\.\d){0,2} and would not match 3 digits after the dot.
To match a digit which could be followed by a dot and 1 or 2 digits after the dot you could use:
^\d+(?:\.\d{1,2})?$
Here the group after the first digit(s) is optional (?:\.\d{1,2})? and the quantifier is specified for the digit \d{1,2}.
Your regex is saying "some digits, followed by 0-2 occurrences of a dot followed by a digit". Spot the mistake? 3.1.4 would match, but 3.14 wouldn't. Contrary to what you state in the question, 3 digits after the point wouldn't match either.
Instead, you would need something like this, assuming that the fractional part should be optional:
\d+(\.\d{0,2})?
Or, anchored and escaped for a string in your language of choice:
"^\\d+(\\.\\d{0,2})$"
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.
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
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 want to have 1 to 5 digits only before the "." and 0 to 2 digits after the "."
this is what I have so far for regex.
^\d{1,5}\.?\d{0,2}$
1.00 -- match
11.01 -- match
111.10 -- match
1111.52 -- match
11111.23 -- match
.12 -- no match ... want to match
123456 -- match ... don't want to match because can take up to 5 integers before decimal
1234567 -- match ... don't want to match because can take up to 5 integers before decimal
can anyone help?
I think you need an alternative between numbers with decimal point and numbers without:
^\d{1,5}|\d{0,5}\.\d{1,2}$
Edit:
Thanks to user1884032 for pointing out the missing parentheses:
^(\d{1,5}|\d{0,5}\.\d{1,2})$
/^(\d{0,5}\.\d{1,2}|\d{1,5})$/
You need this:
^\d{1,5}\.\d{0,2}$
I've removed the ?, which was making the dot optional.
Saying you want to match .12 contradicts your spec - that doesn't have 1-5 digits before the dot. Did you mean {0-5}?
Better late than never:
^([\d]{0,5})(\.[\d]{1,2})?$
Two groups:
One matching from 0 up to 5 digits,
The other from 1 up to 2 decimals, allowing 0.12 as .12.
Simple and perfect :)
The easiest way I achieved in python3 is this -
import re
p = re.compile(r'^\d{2}.\d{2}')
print(p.findall('12.59, 11.789, 10.6 lords a-leaping'))
output - ['12.59']
There are two separate issues:
If you want to match 0 to 5 digits before the "." so you'll need the first part to say d{0,5} instead of d{1,5}.
Instead of having an optional "." followed by two more characters, you should have the "." if there are characters after it, but no dot if there are no characters after it. With the following, you will only end up with seven digits if the last two come after the "."
^\(d{0,5}\.\d{0,2})|(d{0,5}\.?)$
which means
EITHER
0-5 digits, then a decimal point, then 0-2 more digits
OR
0-5 digits, followed by an optional decimal point
This format will also detect legitimate numbers within that range that you didn't list in your test, such as 12345. and 0.12
Try this:
/^\d{0,5}\.\d{0,2}$/
I am new at using the regex.
I want to allow strings with following format:
(Any number of digits).(two digits after .)
e.g. 11.34, 111.78, 132323.78
Can anybody please help me in this?
That would be \d+\.\d\d
\d means any digit
+ means one or more
\. means a literal dot.
EDIT
if you actually mean "two or more digits after" as Mu Mind suggests try the following:
\d+\.\d\d+
\d+\.\d\d
That will match 1 or more digits, then a dot, then 2 digits. (those last two digits must exist in this case.
Fixed: Extra dot