Reputation: 2997
I'm using this regex [+-]?(\d+((\.|\,)\d*)?|(\.|\,)\d+)([eE][+-]?\d+)?
against the following list of numbers:
1
1.2
1.0
1,0
1wer,043
1dd.44
1D
My regex matches for every example in the list posted.
This is ok for the first set (1
, 1.2
, 1.0
, 1,1
) but it also returns a match for the second set (1wer,043
, 1dd.44
, 1D
).
How can I update my regular expression to exclude the last 3 examples?
Upvotes: 2
Views: 1303
Reputation: 52185
The problem seems that you are not escaping the period character. This in turn translates to match any character. I've changed your expression to:
^[+-]?(\d+((\.|\,)\d*)?|(\.|\,)\d+)([eE][+-]?\d+)?$
which should do what you are after.
An example is available here.
The extra ^
and $
at the beginning and end respectively should ensure that the string contains only numeric data. If this is not what you are after, that is, you want to use that expression to extract numerical data, then you can simply omit them.
As per @nhahtdh's comment, your expression can be improved by putting .
and ,
in character class:
^[+-]?(\d+([.,]\d*)?|[.,]\d+)([eE][+-]?\d+)?$
Upvotes: 1
Reputation: 341
You should use begin (^) and end ($) metacharacters
^[+-]?(\d+((.|\,)\d*)?|(.|\,)\d+)([eE][+-]?\d+)?$
Upvotes: 0