How do you escape a hyphen as character range in a POSIX regex

后端 未结 2 384
情深已故
情深已故 2020-12-03 23:47

I have a csv file full of values such as this:

0.00145423,3.03795e-05

I wanted to check that all the lines were consistent so I tried to gr

相关标签:
2条回答
  • 2020-12-04 00:18

    The way to include a literal - in a character list is to put it in the first or last position of the bracket expression, exactly as shown in the answer at: Get final special character with a regular expression.

    From POSIX 9.3.5 RE Bracket Expression: The <hyphen> character shall be treated as itself if it occurs first (after an initial '^', if any) or last in the list, or as an ending range point in a range expression.

    Some tools might have additional ways of doing it with some kind of escaping but you're always safe to just put it first or last. Note that - isn't the only character that has different behavior depending where it shows up in a bracket expression. Consider ], and ^ as well.

    0 讨论(0)
  • 2020-12-04 00:28

    Remember that - is a range operator, so \-\ matches any character in the range \ to \, which is exactly a \.

    If you move it to the end, it'll loose its meaning as a range, that's why it works.

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