Task:
MATCH:
3.45
5,4
.45
3e4
,54
4
4.
4,
DON'T MATCH:
4,5e
2e
.3.
2e,4
,4.
d34
2.45t
2,45.
Currently i came up with the following:
(?<=\s|^)[-+]?(?:(?:[.,]?\d+[.,]?\d*[eE]\d+(?!\w|[.,]))|[.,]?\d+[.,]?\d*(?!\w|[.,]))\b
That works for almost everything, except 2 last numbers (4. and 4,) and got stucked
You may use
(?<!\S)[-+]?[0-9]*(?:[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?|(?<=\d)[,.])(?!\S)
See the regex demo
Details
(?<!\S)
- start of string or a whitespace must appear immediately to the left[-+]?
- an optional +
or -
[0-9]*
- 0+ digits(?:[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?|[,.])
- either
[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?
- an optional .
or ,
, then 1+ digits, then an optional sequence of e
or E
, followed with an optional .
or ,
and 1+ digits|
- or(?<=\d)[,.]
- a dot or comma only if preceded with a digit (to avoid matching standalone .
or ,
)(?!\S)
- end of string or a whitespace must appear immediately to the right.Regex graph: