^((?=.*[1-9]|0)(?:\d{1,3}))((?=.*\d)(?:\.\d{3})?)*((?=.*\d)(?:\,\d\d){1}?){0,1}$
I actually think this regular expression is very long, and mayby could be shorter. The problem is i'm not very good with regular expressions and therefore I ask you for help.
Online regex tester http://regexr.com/3a3mk
My rules:
Positive results
Negative results
[EDIT] Added more negative results
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
Definition and Usage. The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \.
The following should suit your needs:
^(?:0|[1-9]\d{0,2})(?:\.\d{3})*(?:,\d{2})?$
Visualization by Debuggex
Demo on regex101
Edited:
^(0|[1-9][0-9]{0,2}(\.[0-9]{3})*)(,[0-9]{2})?$
matches:
^
beginning of line
[1-9]
just one non-zero digit
[0-9]{0,2}
between 0 and 2 digits
(\.[0-9]{3})*
zero or more lots of a period and 3 digits
(0 | [1-9][0-9]{0,2}(\.[0-9]{3})*)
either (i) a zero or (ii) up to three digits (the first not a zero) followed by blocks of zero or more lots of a period followed by three digits
(,[0-9]{2})?
zero or one lots of a comma and 2 digits
$
end of line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With