i'm trying to build an expression to check decimals as below:
valid number with 6 digits and/or precision of 1-4 digits
---------
656545
452568.1
574896.001
698547.2558
Not valid numbers:
---------
65456.23 - less than 6 main digits
6542166 - more than 6
652541.23165 - more than 4 digits of precision
So far i've made this:
\b\d{6}\.?\d{0,4}?
but it matches also numbers with more than 6 digits and/or more than 4 digits of precision.
Any help?
If I think I understand you, from a numerical perspective, something like this:
No pad: (?<![\d.])[1-9]\d{5}(?:\.\d{0,3}[1-9])?(?![\d.])
Demo: https://regex101.com/r/68gLUF/1
Zero's padded: (?<![\d.])0*[1-9]\d{5}(?:\.\d{0,3}[1-9]0*)?(?![\d.])
Readable version:
(?<! [\d.] ) # Digit boundary
[1-9] \d{5} # Required, Valid 6 digit whole number 100,000 - 999,999
(?: # Optional, Valid decimal fraction .0001 - .9999
\.
\d{0,3}
[1-9]
)?
(?! [\d.] ) # Digit boundary
\b\d{6}\.?\d{0,4}?
The issue here is that the decimal point is optional. This allows for matching a 10 digit number (first six digits, and then 0 to 4 more)... and possibly even more afterwards as there is no anchoring after \d{0,4}?.
Instead:
\b\d{6}(\.\d{1,4})?(?!\.)\b
This way, the decimal point is not optional if there are decimals in the number. The (?!\.) (a negative look-ahead pattern) at the end ensures that the number is not followed by a dot.
If the number occurs on a line by itself (or alone in a string or record, or whatever you have):
^\d{6}(\.\d{1,4})?$
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 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})$"
I want a regular expression that will accept only floating point numbers from 0 to 9 and minus sign.
Please help.
^[-+]?[0-9]*\.?[0-9]+$
^ - start of string
[-+]? - 0 or 1 sign indicator
[0-9]* - 0 or more integers
\. - the character . (. is used in regex to mean "any character")
[0-9]+ - 1 or more integers
$ - the end of the string
If you are instead using the comma as a decimal seperator, use , instead of \.
If you are using both/either, you can use [.,]
Try ^[-+]?[0-9]*[.,]?[0-9]+$.
This regular expression will match an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or followed by one or more digits (an integer).
Source: http://www.regular-expressions.info/floatingpoint.html - altered to work with commas as decimal separator
Please help me make regular expression for positive decimal number with 0, 1 or 2 decimal places. It must allow comma and dot.
For example it must allow:
0,01
0.01
0,1
1
1.1
1,11
but not allow:
-1
0.0
0,00
.01
0
1,111
1.111
I have this
/(^\d*(?:\.|\,)?\d*[1-9]+\d*$)|(^[1-9]+\d*(?:\.|\,)\d*$)/
but I can`t find how to disallow more than 2 decimal places.
UPDATE
Men, I must reject 0.0, 0 and etc.
Edit 2: now disallows exactly 0,0.0, etc.
This matches at least one digit before the decimal place, followed by an optional decimal place, followed by 0-2 digits.
The negative lookahead looks for any flavor of absolute zero and prevents a match.
^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$
This is the raw regex, so you'll need to escape it appropriately for your language. (For example, in some languages you need to double the \ slashes as \\.
/^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$/
What you've got so far seems unnecessarily complicated to me. How about just
/^\d+([.,]\d{0,2})?$/
This is correct for every test case in the OP except for:
0.0
0,00
0
I don't see why you'd reject these.
you can use the bracket notion to limit the number of digits:
\d{0,2} would mean any run of digits from a minimum of 0 to a maximum of 2
/^\d+([.,]\d{1,2})?$/
this will properly disallow these "unformatted" numbers .01, 3., etc.
if we have zero decimal place digits we probably as well don't want the decimal separator.
This will do what you want.
I've added whitespace and comments and parentheses to clarify it:
( #
( 0*[1-9]\d* ) # a non-zero integer portion, followed by
( [\.,]\d{1,2} )? # an optional fraction of 1 or 2 decimal digits
) #
| # OR
( #
( 0+ ) # a zero integer portion, followed by
( # an mandatory non-zero 1-2 digit fraction, consisting of
[\.,] # a decimal point
( # followed by
( 0[1-9] ) # a 0 followed by a 1-9,
| # OR
( [1-9]\d? ) # a 1-9 followed by an optional decimal digit
)
)
The regular expression is suboptimal it something like 0000000000.01 will backtrack when it doesn't find a non-zero digit following the zeros in the integer portion, but it should work.
I'm trying:
\d{3}|\d{11}|\d{11}-\d{1}
to match three-digit numbers, eleven-digit numbers, eleven-digit followed by a hyphen, followed by one digit.
But, it only matches three digit numbers!
I also tried \d{3}|\d{11}|\d{11}-\d{1} but doesn't work.
Any ideas?
There are many ways of punctuating phone numbers. Why don't you remove everything but the digits and check the length?
Note that there are several ways of indicating "extension":
+1 212 555 1212 ext.35
If the first part of an alternation matches, then the regex engine doesn't even try the second part.
Presuming you want to match only three-digit, 11 digit, or 11 digit hyphen 1 digit numbers, then you can use lookarounds to ensure that the preceding and following characters aren't digits.
(?<!\d)(\d{3}|\d{11}|\d{11}-\d{1})(?!\d)
\d{7}+\d{4} will select an eleven digit number. I could not get \d{11} to actually work.
This should work: /(?:^|(?<=\D))(\d{3}|\d{11}|\d{11}-\d{1})(?:$|(?=\D))/
or combined /(?:^|(?<!\d))(\d{3}|\d{11}(?:-\d{1})?)(?:$|(?![\d-]))/
expanded:
/ (?:^ | (?<!\d)) # either start of string or not a digit before us
( # capture grp 1
\d{3} # a 3 digit number
| # or
\d{11} # a 11 digit number
(?:-\d{1})? # optional '-' pluss 1 digit number
) # end capture grp 1
(?:$ | (?![\d-])) # either end of string or not a digit nor '-' after us
/