Whether to escape ( and ) in regex using GNU sed

后端 未结 4 1742
清歌不尽
清歌不尽 2020-12-08 14:53

I\'ve noticed several posts on this site which say that with gnu sed you should use ( and ) in regex rather than \\( and \\)

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 15:45

    Originally sed, like grep and everything else, used \( to indicate grouping, whereas ( just matched a literal open-paren.

    Many newer implementations of regular expressions, including egrep and perl, switched this around, so \( meant a literal open-paren, and ( was used to specify grouping.

    So now with gnu sed, ( is a special character; just like egrep. But on other systems (e.g. BSD) it's still the old way, as far as I can tell. Unfortunately this is a real mess, because now it's hard to know which one to use.

提交回复
热议问题