Hyphen and underscore not compatible in sed

前端 未结 4 2015
傲寒
傲寒 2021-01-18 15:39

I\'m having trouble getting sed to recognize both hyphen and underscore in its pattern string.

Does anyone know why

[a-z|A-Z|0-9|\\-|_]
4条回答
  •  臣服心动
    2021-01-18 16:06

    As mentioned, you don't need anything to separate your ranges in a bracket expression. All that will do is adding | to the characters matched by the expression.

    Then, to add a hyphen, you can put it as the first or last character in the expression:

    [a-zA-Z0-9_-]
    

    And finally, ranges like a-z don't necessarily mean abcd...xyz, depending on your locale. You could use a POSIX character class instead:

    [[:alnum:]_-]
    

    Where [:alnum:] corresponds to all alphanumeric characters of your locale. In the C locale, it corresponds to 0-9A-Za-z.

提交回复
热议问题