Regex.Split on plus and minus sign

前端 未结 2 1648
不知归路
不知归路 2021-01-21 07:43

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

相关标签:
2条回答
  • 2021-01-21 08:06

    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.

    0 讨论(0)
  • 2021-01-21 08:08

    Tried to escape signs like +?

    And why not a RegEx like /\d+\.?\d+/ ? This won't split it but return the numbers.

    0 讨论(0)
提交回复
热议问题