Regex for numeric value that could contain comma & dots - regex

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/

Related

validate string data using regex which allowed only numbers and - (hyphen) of length 6 but hypen should not be the end and the digits not same

Validate the string using the regex which has the - (hypen)
Requirement is : string contains only digits and - (hyphens) and not end with - (hyphen) and all other digits not be the same.
^([0-9-])(?!\1+$)[0-9-]{5}$
The above one allow only digits and hyphen but its not restricted end with hyphen and check all other digits are same.
ex:
1111-1 Not allowed because all are same digits
1111-2 Allowed
11112- Not allowed as its end with - Hypen
-12345 Not allowed as its start with - hypen
You might write the pattern as
^(\d)(?!(?:\1|-)+$)(?!\d*-\d*-)[\d-]{4}\d$
Explanation
^ Start of string
(\d) Capture a single digit in group 1
(?! Negative lookahead
(?:\1|-)+$ Check that to the right there is not only the group 1 value or hyphens
(?!\d*-\d*-) Assert not 2 hyphens
) Close lookahead
[\d-]{4} Match 4 digits or hyphens
\d Match a digit
$ End of string
Regex demo
If there should be at least 1 hyphen:
^(\d)(?!(?:\1|-)+$)(?=\d*-)[\d-]{4}\d$
Regex demo
My 2 cents to allow [01] hyphens:
^(?=.{6}$)(\d)(?=.*(?!\1)\d)\d+(?:-\d+)?$
See an online demo

Regular Expression Scope Too Large

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

Regular Expression to match a string with an even number of 1's and starts and ends with a 0

I have been trying to create a regular expression for the statement above. The best solution I have come up with so far is 0(0*(10*10*)*0)?. This allows for a single zero and multiple zero's to be matched which I believe is correct but I am just wondering if there is a simpler solution.
You may use a tiny bit more efficient pattern like
^0+((0*10*1)*0+)?$
With non-capturing groups:
^0+(?:(?:0*10*1)*0+)?$
And if you need to allow an empty string:
^(?:0+(?:(?:0*10*1)*0+)?)?$
See the regex demo. As the zeros come before ones in the repeated group, there is a little less backtracking when it comes to matching last zeros.
Details
^ - start of string
0+ - one or more 0s
((0*10*1)*0+)? - an optional sequence of
(0*10*1)* - 0+ repetitions of 0+ zeros, 1, 0+ zeros and then again 1
0+ - one or more 0s
$ - end of string.
Try the following regex:
^0(?=(?:(?:[^1]*1){2})+[^1]*0$).*
Details:
^0 - Start of string and (leading) 0.
(?= - Positive lookahead for:
(?: - A non-capturing group (outer).
(?: - Another (inner) non-capturing group, containig:
[^1]*1 - A (possibly empty) sequence of chars other than 1 and 1.
){2} - Occurring 2 times (a pair of ones).
)+ - This group (pairs of ones) can occur several times (even number of ones).
[^1]* - A (possibly empty) sequence of chars other than 1
0$ - 0 and the end of string (terminating 0).
) - End of positive lookahead.
.* - Match the whole string.
If you accept that no 1 chars are present, change + after the outer capturing
group to *.
For a working example see https://regex101.com/r/FvP0Ud/1
You can experiment with this regex, adding / removing 1 chars, at the start
/ end / in the middle of the string and observe whether the string is matched
or not.
Another detail: The above regex allows also digits other than 0 and 1
and generally all characters. If you want to limit the allowed chars to
just 0 and 1, change [^1] to 0 (in both places).

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

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.