I am after a regex that allows a number to be a maximum length with decimal places.
It should allow 16 numbers maximum and 3 decimal places, however the 16 max should include the decimals but not the . character. I've tried and have this so far: ^(?=^[\d\.].{0,16}$)[0-9]+(\.[0-9]{1,3})?$, which is close except it allows 17 single numbers, when it should only accept 16. If I change {0,16} to {0,15} it then breaks the decimals.
Accepted values:
123
123.3
123.45
123.456
1111111111111.345 (totally 16 numbers)
11111111111111.45 (totally 16 numbers)
Rejected:
123.1234
11111111111111111 (17 characters)
1111111111111111.
111111111111111.56 (15 characters and 2 numbers = 17)
Thanks in advance.
Your regex is fairly close, you may use this regex:
^(?=(?:\d\.?){0,16}$)\d+(?:\.\d{1,3})?$
RegEx Demo
RegEx Details:
(?=(?:\d\.?){0,16}$): is positive lookahead to assert that we have 0 to 16 digits in input where dot is optional.
\d+(?:\.\d{1,3})?: Match an integer or a decimal number with 1 to 3 digits.
Related
I need a regular expression to extract a specific set of numbers from a string. The string could contain letters, special characters and spaces.
Input examples:
This is a test 99 12 3456
This is test 2 94123456
This is test 3 357 95123456
This is test 4 35797123 456
And so on…
The regex should look for a string of 8 numbers starting with 94 or 95 or 96 or 97 or 99 followed by 6 more numbers.
example:
94<6 more numbers here>
95<6 more numbers here>
96<6 more numbers here>
97<6 more numbers here>
99<6 more numbers here>
or 11 numbers starting with 357 followed by 94 or 95 or 96 or 97 and 6 more numbers.
example:
35794<6 more numbers here>
35795<6 more numbers here>
35796<6 more numbers here>
35797<6 more numbers here>
35799<6 more numbers here>
So the output should either be 8 numbers, or 11 numbers. Less than 8 or more than 11 is not a valid output. Also anything between 8 and 11 is not valid.
Hope this makes it more clear
Thanks for your help
Maybe this:
(357|94|95|96)[\d ]{6,}
Which means "357" or "94" or "95" or "96" followed by at least six digits and/or spaces. I wasn't sure exactly what you want. It would be better just to post the exact input and output desired.
If you’re working in an environment that supports lookbehinds, you can ensure you’re not matching a partial number by using a negative lookbehind and negative lookahead:
/(?<!\d)(?:357)?9[4-79]\d{6}(?!\d)/
(?<!\d): Negative lookbehind (ensure there isn’t a digit before the matching expression)
(?:357)?: Create a non-capturing group of 357 to attach an optional quantifier (match 357 zero to 1 times)
9: Match 9
[4-79]: Character set with range 4-7 and 9 (match one of these characters)
\d{6}: Match a digit exactly 6 times
(?!\d): Negative lookahead (ensure there isn’t a digit after the matching expression)
This regular expression will do it if you remove the spaces from the input first: 3579[4-9](?:\d{8}|\d{6})
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})?$
I want except some numbers in different syntax and I am trying to find the best Regex for this task/match.
First some valid numbers:
0.01
0.2
0.38
45
165.6
52732.08
999999999.99
And here some invalid numbers:
.01
.2
.50
.85
45.
45.0
45.00
00045.0
124.60
000124.60
124,6
000053853.01
999.999.999,99
999999999,99
After several tests I have created the following Regex:
^[1-9]?\d{1,9}\.?\d{1,2}(?<!0)$
But I always struggling on the number: 000058723.01
Any ideas? Thanks.
You can use this regex:
^(?!0+\d)\d+(?:\.(?![1-9]*0+$)\d{1,2})?$
Or:
^(?:0+|[1-9]\d*)(?:\.(?![1-9]*0+$)\d{1,2})?$
RegEx Demo
Try this pattern:
^((?:0|[1-9]+)(?:\.(?:\d+?[1-9]|[1-9]))?)$
Demo
You accept four kinds of input:
A number with no decimal places and without leading zeroes: [1-9]\d*
Zero followed by a dot followed by digits (without trailing zeroes): 0\.\d*[1-9]
A decimal number without leading or trailing zeroes: [1-9]\d*\.\d*[1-9]
Zero: 0
Putting the four together:
^([1-9]\d*|0\.\d*[1-9]|[1-9]\d*\.\d*[1-9]|0)$
Here is a fixed version of your regex:
^(?!0{2,})\d+(?:\.\d{1,2}(?<!0))?$
Here, initial 2 or more zeros are not allowed with the lookahead (?!0{2,}), and the decimal part is made optional within a non-capturing group (?:\.\d{1,2}(?<!0))?.
See demo
In case you do not want to match 0, you can exclude this in the negative lookahead:
^(?!0{2,}|0$)\d+(?:\.\d{1,2}(?<!0))?$
^^
See Demo 2
A number with optional decimals is composed from two pieces: the integer part and the optional decimal part that starts with a dot.
The integer part is either zero (0) or a sequence of digits that start with 1..9 (no 0) and can continue with zero or more digits:
0|[1-9][0-9]*
If you need to impose an upper limit on the integer part's length then replace * with {,n} where n is the maximum allowed length minus 1.
The decimal part starts with a dot (.) followed by zero or more digits and followed by one of 1..9 (no 0 allowed at the end).
The expression is:
\.[0-9]*[1-9]
Now let's combine them:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
What I added when I joined the pieces:
^ - match the start of the string; without this the regex matches 45.0 from 00045.0;
parentheses around the integer part because of the lower precedence of |;
parentheses around the decimal part, followed by ? to signal the entire decimal part is optional;
$ - match the end of the string to avoid matching 124.6 from 124.60.
Remarks
The above regex was designed to match your examples. However, please notice that most programming languages allow most or all of the numbers you put in the "invalid" section and use a dot (.) as decimal separator. And many languages provide library functions that are able to parse the numbers that use a comma (,) as decimal separator.
Numbers without integer part (.85), without digits after the dot (45.) ore with trailing zeros (45.0) are valid and are interpreted without ambiguity.
The only troublemaker is the leading zero (00045.0). For integer numbers, most of the times it is a signal that the number is represented in base 8 while for real numbers it is simply ignored.
RegEx for number with in range 1 to 100000 with decimal upto two digits.
-1, 0 should not be allowed.
1 to 100 000 allowed e.g. 1.00, 13.99, 100.45,9999.34, 99999.99, 100 000
use this :
^(?:[1-9][0-9]{0,4}(?:\.\d{1,2})?|100000|100000.00)$
demo here : http://regex101.com/r/bY1yT2/2
You could try the below regex to match the numbers from 1 to 100000 with optional decimal upto two digits.
^(?:100000|[1-9]\d{0,4})(?:\.\d{1,2})?$
DEMO
\d{0,4} Matches upto four digits.
(?:\.\d{1,2})? Matches an Optional decimal and the following digits. Number of digits following must be 1 or 2.
Can a Regex be written to find numbers in a string that are greater than x?
Say for example x = 1800
Can we find for that a number > 1800 which is in the strings: "3,000.00/month" | "$2800" | "150,000.00a month" | "only $1900" etc? The regex would find 3,000.00, 2800, 150,000.00 and 1900 because they are all greater than 1899.
What is this pattern that elludes me..
You can find any well-formatted number greater than a number with a regular expression, but it does get more complicated as the numbers get bigger.
Let's start with a simple example where x = 11, and you wanted to find any number in a string greater than 11. The regex you'd write would follow a pattern similar to the solution below:
(\d{3,}|[2-9]\d|1[2-9])(\.\d+)?|11\.\d*[1-9]\d*
REY
What is the purpose of first three inner alternations? The first captures any three or more digit number, the second any two digit number where the digit in the 10's place is 2-9, and the last any two digit number where the digit in the 10's place is 1 and the digit in the 1's place is 2-9. Then it optionally captures any decimal decimal digits.
The second outer alternation actually matches x in the integer part, but then checks the decimal part to ensure it is somehow greater. If you had a decimal part of 12 for x then you'd just do 120*\d*|1[3-9]\d*|2\d*.
There are two situations that will make the above pattern not work:
When a number contains commas
When a number starts with zeroes.
The example below deals with those cases as well and solves for x = 1800.
#FIND any number > 1800
(?=[1-9]) # ensure number doesn't begin with a zero
(
(\d{1,3},(?=\d{3}))*\d{2,3},\d{3}| # any number that has >= 5 digits with commas
\d{5,}| # any number that has >= 5 digits without commas
[2-9],?\d{3,}| # any 4 digit number that starts with 2-9
1,?9\d{2}| # any 4 digit number that starts with 1 then 2-9
1,?8[1-9]\d| # any 4 digit number that starts with 18 then 1-9
1,?80[1-9] # any 4 digit number that starts with 180 then 1-9
)
(\.\d+)? # any decimal digits
|1,?800\.\d*[1-9]\d* # any number whoses integer = 1800,
# ... then has a decimal with a non-zero digit.
REY
At the top of the regex is uses a look ahead to ensure a number actually starts with 1-9. Without it a number like 00005 would be confused as a five digit number.
The inner group finds all integer matches greater than 1800, just like the first example tried to match numbers greater than eleven. Probably, the only non obvious alternation is the first - (\d{1,3},(?=\d{3}))*\d{2,3},\d{3}. To find a 5+ digit number it matches 1-3 digits followed by a comma, then 2-3 digits, a comma and then the last 3 digits. Without the look ahead it would incorrectly match a non-number such as 234,23,412.
If you're going to be picky about commas, like I am being, then you're not going to be able to integrate comma and non-comma cases such as with 1,?9\d{2} => 1914 & 1,914. Having multiple ,? would lead to incorrect matches such as 1000,050, when trying to find a number greater than a million.
You could solve it in two steps: first find a number and then apply a regex to it that checks if it is greater than you number x.
As an example:
here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:
^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$
where
[3][1-9] - matches numbers from 31 to 39
[4-9][0-9] - matches numbers from 40 to 99
^[1-9][0-9]{2,4} - matches numbers from 100 to 9999
The last bit ^[1-9][0-9]{2,4} can be changed to ^[1-9][0-9]{2,} to match any number greater than 100