I have a string 1.5(+1.2/-0.5)
. I want to use Regex to extract numerical value: {1.5, 1.2, 0.5}
.
My plan is to split the stri
The dash is a special character when inside square brackets in a regexp. It means a range: [a-z]
means any character from a
to z
. When you wrote [(/+-)]
, it would actually mean (
, or any character from +
to )
. The error comes from the fact that in ASCII ordering )
comes before +
, so a character range [+-)]
is invalid.
To fix this, dash must always come first or last when in brackets, or it needs to be backslashed.
And I agree, I'd probably use a global regexp to pick out [0-9.]+
, and not a split to cut on everything else.
Tried to escape signs like +?
And why not a RegEx like /\d+\.?\d+/
? This won't split it but return the numbers.