I was wondering how it's possible to get a full match of positive and negative numbers in a given dataset that also has non-digit symbols. I have a bunch of currency transactions and I'd like to exctact just the numeric value.
With the following regex, I get all the numbers correctly but I can't find a way to get the "-" sign.
Any idea why?
(\d+)(,(\d{3}))*((?=[,.-](\|$))|(\.\d+))
Dataset:
null
null
($7.08 CAD)
(-$1.06 CAD)
(€50.03 EUR)
($1,024.38 CAD)
(-$1,024,309.06 CAD)
(€50.03 EUR)
Desidered output:
7.08
-1.06
50.03
1,024.38
-1,024,309.06
50.03
regex example https://regex101.com/r/Nw8EdE/2
One option is to use 2 capturing groups for the optional -
and the digits part and match the optional euro or dollar sign in between [$€]?
Then you could concatenate the first and the second capturing group as a result.
(-?)[$€]?(\d+(?:[,.]\d+)*)
Explanation
(-?)
Capture group 1, match optional -
[$€]?
Optionally match 1 of the listed chars(
Capture group 2
\d+
Match 1+ digits(?:[,.]\d+)*
Repeat 0+ times mathching either a ,
or .
followed by 1+ digits)
Close group