I want to create a regular expression for all sorts of numbers i.e whole numbers (+ve and -ve) and decimal numbers (+ve and -ve) with or without commas.
For e.g the regular expression should cover following number formats.
111 1.11 1,111 1,111.01 1,111,111.01
+111 +1.11 +1,111 +1,111.01 +1,111,111.01
-111 -1.11 -1,111 -1,111.01 -1,111,111.01
I have created two regular expressions for handling my scenario.
"^(\\+|-)?[0-9]\\d*(\\.\\d+)?$" // handles whole numbers with decimals
"^(\\+|-)?[0-9]\\d*(\\,\\d+)*?$" // handles whole numbers with commas
Now, I want to merge these two regular expressions in order to address my requirements.
Can anyone help me?
Thanks in advance.
What about this one:
^[+-]?\d+(,\d+)?(\.\d+)?$
You can see it working here.
Here is my solution that allows only 3 digits between comma:
^[+-]?\d{1,3}(?:,\d{3}|\d+)*(?:\.\d+)?$
Explanation:
^ : start of string
[+-]? : optional + or -
\d{1,3} : 1 to 3 digits (before the first optional comma)
(?: : non capturing group
,\d{3} : a comma followed by 3 digit
| : OR
\d+ : 1 or more digits
)* : group present 0 or more times
(?: : non capturing group
\.\d+ : decimal dot followed by 1 or more digits
)? : optional
$ : end of string
You may merge these 2 patterns of yours like
^[+-]?[0-9]+(?:,[0-9]+)*(?:[.][0-9]+)?$
See the regex demo
Details:
^ - start of a string
[+-]? - an optional + or -
[0-9]+ - 1 or more digits
(?:,[0-9]+)* - zero or more sequences of:
, - a comma
[0-9]+ - 1 or more digits
(?:[.][0-9]+)? - an optional sequence of:
[.] - a dot
[0-9]+ - 1+ digits
$ - end of string
A more restrictive regex to only allow 3-digits in groupings will look like
^[+-]?[0-9]{1,3}(?:,[0-9]{3})*(?:[.][0-9]+)?$
^^^^^ ^^^
And if you want to also match whole parts with no thousand separators:
^[+-]?(?:[0-9]{1,3}(?:,[0-9]{3})*|[0-9]+)(?:[.][0-9]+)?$
Related
I have been trying but without success
I need a regular expression for validating numbers that could contain dots and commas,
the number should be positive and there should be max of two numbers after the comma
Valid cases would be:
1000 - valid
1,000 - valid
1,000.22 - valid
-2 not valid
1,000.233 not valid
0 not valid
1.00,22 - not valid
Language is javascript
let valid =["1000","1,000","1,000.22"];
let notValid = ["-2","1,000.233 ","0","1.00,22"];
let rge = /^[1-9]+\d*(,\d{3})*(\.\d{1,2})?$/;
for(let x of valid)
console.log(x," è valida? ",rge.test(x));
for(let x of notValid)
console.log(x," è valida? ",rge.test(x));
Above there is a possible solution in Javascript, you haven't specified the language.
\d are numbers in the range [0-9]
The full stop . is a metacharacter (it means any character), to refer to the character . you have to escape it thereby \.
+ means at least 1 or more times
* means 0 or more times
? means 0 or 1 time
{1,2} means match minimum 1 time, maximum 2 times
The starting ^ and final $ refer to a exact matching otherwise you could have a partial matching of the string
A few assumptions:
Invalid: '123456789.12' and '12345,123.12'
I think the following does what you are after:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*(?:\.\d\d?)?)$
See the online demo
^ - Start-line anchor.
[1-9] - A single digit in the range 1-9.
(?: - Open a non-capture group:
\d* - 0+ Digits to allow any integer.
| - Or:
\d{0,2} - Between 0 to 2 digits;
(?:,\d{3})* - Followed by a non-capture group to allow any 0+ times a comma followed by 3 digits.
(?:\.\d\d?)? - Followed by an optional non-capture group to allow up to two decimals.
)$ - Close non-capture group and match the end-line anchor.
Or, if you also want to allow any integer followed by decimals (e.g: '123456789.01') you may change this to:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*)(?:\.\d\d?)?$
I think this regex should do the trick:
[1-9][\d,]*(\.\d{1,2})?
[1-9] - matches one character between 1 and 9 at the beginning (required to not match 0)
[\d,]* - matches zero or more digits or commas
(\.\d{1,2})? - zero or one group of a dot and one or two digits
For testing regexes I do recommend https://regex101.com/
I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.
Looking for some help on regex expression for below use case.
My input field should allow only comma separated values of either just digits, or a sequence of special characters (*) followed by digits. It should able to allow below combinations:
1234,***1234,3456
***1234,***3456,12345567
1234,3456
***1234,***3456
The digits only case can have max 10 digits. The case with special characters and digits should have 3 asterisks followed by 4 digits.
This validates a string of comma separated items, where each item is either a number of up to 10 digits, or three * followed by a 4 digit number:
/^(?:\d{1,10}|\*{3}\d{4})(?:,(?:\d{1,10}|\*{3}\d{4}))*$/
Explanation:
^ - anchor at start of string
(?: - non capture group start
\d{1,10} - 1 to 10 digits
| - logical OR
\*{3}\d{4} - three * followed by 4 digits
) - non capture group end
(?: - non capture group start
,(?:\d{1,10}|\*{3}\d{4}) - a comma, followed by same ORed pattern as above
)* - non capture group end, whole group has zero to many repetitions
$ - anchor at end of string
I have created a (mostly) working regular expression that accepts any number > 0 or <= 12, allowing up to two decimal places. The problem is that it also accepts numbers between 12-13 such as 12.25, and also 0.
My regular expression pattern is /^\b(0*([0-9]|1[0-2]))\b\.?[0-9]{0,2}$/
How can I change this to prevent 0 or numbers greater than 12 from being accepted?
You may use
^(?=[^1-9]*[1-9])0*(?:(?:\d|1[01])?(?:\.[0-9]{0,2})?|12(?:\.0{0,2})?)$
See the regex demo and the regex graph:
If a digit after a decimal separator symbol is required, replace {0,2} with {1,2}:
^(?=[^1-9]*[1-9])0*(?:(?:\d|1[01])?(?:\.[0-9]{1,2})?|12(?:\.0{1,2})?)$
Details
^ - start of string
(?=[^1-9]*[1-9]) - a positive lookahead that requires one non-zero digit (a digit from 1 to 9) after any chars other than digits from 1 to 9
0* - any 0+ leading zeros
(?: - start of a non-capturing group:
(?:\d|1[01])?(?:\.[0-9]{1,2})? - 0 to 11 numbers (matched optionally, see the ? after the first closing parenthesis) followed with an optional sequence of . and 1 to 2 digits
| - or
12(?:\.0{1,2})? - 12 optionally followed with . and 1 to 2 digits
) - end of a non-capturing group
$ - end of string
I'm matching whole and fractional numbers with the following pattern:
(0|[1-9][0-9]*)(.[0-9]+)?
It matches for example the numbers:
0
1
12
12.1
12.12
But it also matches:
0.0
0.00
1.00
1.10
My question is how to modify the pattern above to disallow the match of numbers with ending zero after the decimal point.
Your pattern (0|[1-9][0-9]*)(.[0-9]+)? matches all examples because the second part (.[0-9]+)? does not take into account that the last match should be 1-9. Note that you have to escape the dot \. to match it literally.
You could update the second part of your pattern to match 0+ times [0-9]* and make sure that the match ends with [1-9]:
^(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$
Regex demo
The pattern matches:
^ Start of string
(0|[1-9][0-9]*) Capture group, match either 0 or 1 followed by 0+ times 0-9
( Optional capturing group to match
\.[0-9]*[1-9] Match ., then 0+ times 0-9 followed by 1-9
)? Close group
$ End of string
If you are for example not referring to the capturing groups for further processing you might make them non capturing (?: instead.
You can use the following regex:
^(0|[1-9][0-9]*)(.(?:[0-9]+[1-9]|[1-9]))?$
demo: https://regex101.com/r/ZtRg49/5
This will not match at all the following elements:
0.0
0.00
1.00
1.10
If you want to allow partial matches (not take into account the ending 0), you can use:
^(0|[1-9][0-9]*)(.(?:[0-9]+[1-9]|[1-9]))?
demo https://regex101.com/r/ZtRg49/4/
Explanations:
I have added the constraint that after the . there is one digit [1-9] or several digits that can contain 0 followed by a non zero digit [1-9].
By removing the ending anchor $ you enable partial matches
My suggestion to this Problem:
^\d+$|[0-9.]*?[1-9]$
Requirements to be fulfilled:
Match whole numbers
Match floating point numbers not ending on 0