Whether to escape ( and ) in regex using GNU sed

后端 未结 4 1729
清歌不尽
清歌不尽 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:51

    Thanks to rocker, murga, and chris. Each of you helped me understand the issue. I'm answering my own question here in order to (hopefully) put the whole story together in one place.

    There are two major versions of sed in use: gnu and bsd. Both of them require parens in basic regex to be escaped when used for grouping but not escaped when used in extended regex. They diff in that the -r option enables extended regex for gnu but -E does so for bsd.

    The standard sed in mac OSX is bsd. I believe much of the rest of the world uses gnu sed as the standard but I don't know precisely who uses what. If you are unsure which you are using try:

    > sed -r
    

    If you get a

    > sed: illegal option -- r
    

    reply then you have bsd.

提交回复
热议问题