How do I create a regular expression that validates if a String is:
- a number
- [optional]
,
decimal point - [required if you eat] another number
Also:
- It does NOT have a dot
.
as a symbol for thousands.
I have tried to create it:
[0-9,]+[^.]
But the results:
22,33 CORRECTO
33.44 INCORRECTO
22,33... CORRECTO // PERO DEBERIA SER INCORRECTO!!!
232.33dfdfd CORRECTO // PERO DEBERIA SER INCORRECTO!!!
Expected results:
22,33 CORRECTO
225,3432 CORRECTO
33.44 INCORRECTO
22,33... INCORRECTO
23, INCORRECTO
Try this expression
Debugx Demo
this should be your expression
remember that if you pass it to a function as a string you must escape the slashes, like so:
This expression accepts negatives, uses
\d
instead of[0-9]
, without creating unnecessary groups, or escaping unnecessary characters:Alternatively, if you want to accept scientific notation (eg:
1.2e+05
):Demo en regex101.com
Some notes on the differences:
Negatives . It is a common mistake to forget that a number can also be negative and start with a
-
. I hope it helps to avoid headaches when the code is already in production.Using
\d
. In some languages,\d
it matches digits from other alphabets . Among the big ones: Python3, those of .Net, Perl, C/C++ (with boost), R, TCL or Oracle. If you want to avoid that detail, there we would replace all the\d
by[0-9]
. But for the rest,\d
it is exactly the same as[0-9]
and visually it is better.Without creating unnecessary groups . When you use parentheses, you're not just grouping, you're also capturing the text that is matched by that group. But there are groups without capturing , whose syntax is , to avoid using unnecessary memory. Also, it's easier for whoever is reading your regex, because it's clear that you're grouping but you don't care about getting that part back. - I know, at first it is difficult to read those and more, which is not a quantifier, but I assure you that in a short time it is read in a row and understood automatically .
(?:subpatrón)
?
:
Without escaping what is not necessary . The comma (
,
) is not a metacharacter in regex. As such, it doesn't need to be escaped (neither\,
, nor[,]
), just the,
in the regular expression is taken as a literal.Java has no option for string literals ☹. It's the only language (that I know of) where you have to escape each slash to use this regex:
Code:
With this regular expression you validate that the numbers are separated by commas for the thousands without decimals.
Example:
You can validate it at http://regexr.com/
This is better because it allows you to put only integers if necessary, and if you put a point it only accepts it, or with decimals, I leave you with some case scenarios
Debuggex Demo https://www.debuggex.com/r/X43uNbM0EERtm8NF
With this it validates that it has 1 or 2 characters after the decimal point.
it is not mandatory to place decimals.
^[0-9]+([.][0-9]{1,2})?$
https://www.debuggex.com/r/qTPyvu3WB9xuUCaj
If I understand your description correctly, the following string would be a valid number:
For that string to be accepted as well, the regular expression would be:
Debugx Demo
^
It is important to include the chain start and chain end anchors$
.Without the anchor
$
the chain232.33dfdfd
as correct when it is not.If
22,
it is not a correct string then the regular expression to use is:Debugx Demo