Regular Expression Scope Too Large - regex

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

Related

Regex for numeric value that could contain comma & dots

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/

Regex expression help to allow complete digits or with 3 special characters followed by 4 digits in comma separated without spaces

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

conditional with no leading zeros in decimal number using regular expression

Below is my regular expression I am working with
^\d+\.?((25)|(5)|(75)|0+)?$ which should be allowing numbers like
0010,10.0,0.0,0,0.25,1.0,22,1.5,11.25,23.75,22.000 etc.
I don't want to allow like
0017.5,0010.0 etc with leading 0s in decimal numbers.
How can I make that?
Provided you may use lookaheads in the regex pattern, you may use
^(?!0+[1-9]\d*\.\d)\d+(?:\.(?:[27]?5|0+))?$
See the regex demo. The (?!0+[1-9]\d*\.\d) negative lookahead will fail the match if the strings starts with one or more zeros and the number is itself a number with a fractional part.
Regex details
^ - start of string
(?!0+[1-9]\d*\.\d) - a negative lookahead that fails the match if, immediately at the start of the string, there is
0+ - one or more 0 chars
[1-9] - a digit from 1 to 9
\d* - 0+ digits
\. - a dot
\d - a digit
\d+ - 1+ digits
(?:\.(?:[27]?5|0+))? - an optional string that matches 1 or 0 occurrences of:
\. - a dot
(?:[27]?5|0+) - 2 or 7 (optionally) and then 5, or one or more 0 chars
$ - end of string.
Give a range to the starting number instead of allowing any number and allow 0 or more numbers after that number but before the comma. Remove the ?..? quantifier because you want to make sure it is a decimal number. To account for whole numbers add |(\d+). And to account for numbers between 0 and 1, add ([0]\.((25)|(5)|(75)|0+)) This becomes:
^(([0]\.((25)|(5)|(75)|0+))|([1-9]\d*\.((25)|(5)|(75)|0+))|(\d+))$

Regular expression for integer with or without commas

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]+)?$

find matches for positive and negative number in arithmetic expression

Given the following arithmetic expression:
-1-5+(-3+2)
There is need to find matches for positive and negative numbers. For that expression expected result is: -1 5 -3 2
I tried to use regex -?\d+(.\d+)? but it returns: -1 -5 -3 2 where -5 is not correct.
Is that possible to build regex pattern to get positive and negative numbers for that case and other similar cases ?
You can use
(?<!\d)[-]?\d*\.?\d+
See the regex demo
Pattern details:
(?<!\d) - a negative lookbehind that fails the match if a digit appears before the currently tested position
[-]? - an optional (1 or 0) minus sign
\d* - 0+ digits
\.? - 1 or 0 dots (a literal dot as it is escaped)
\d+ - 1+ digits
Note that \d*\.?\d+ allows .456 values, if you do not need that, just use \d+(?:\.\d+)?.
If the lookbehind is not supported, use a capturing group with alternation to check if the - is not at the start of the string or before another digit:
(?:^|\D)([-]?\d*\.?\d+)
See another demo (the necessary value is in Group 1).
/((?:^|\+|-|\()(-?\d+))/
You don't say what language/script you're using, but here's a PHP example:
$string = '-1-5+(-3+2)';
preg_match_all('/(?:^|\+|-)(-?\d+)/', $string, $matches);
print_r($matches[1]);
Outputs:
Array
(
[0] => -1
[1] => 5
[2] => -3
[3] => 2
)
PHP Sandbox
EXPLANATION: There's two grouped patterns:
/
(?:^|\+|-|\()
(-?\d+)
/
The first group is a non-capturing group ?:, and an alternation, that is, match any character in the list. There's 4 characters in that list:
^ line start
\+ literal plus
- minus
\( literal open paren
The first group is there to make the optional - in the second group stand out. Every digit (positive or negative) will be preceded by one of the characters listed in the first group, now match all that follows including the - if present.