sed -e 和 sed -E 的区别 如果讨厌在(前面加\,那么就使用 -E参数吧

官方说明:

  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).


详细解释

5.2 Basic (BRE) and extended (ERE) regular expression

Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the default in sed (and similarly in grep). Use the POSIX-specified -E option (-r--regexp-extended) to enable Extended Regular Expression (ERE) syntax.

In GNU sed, the only difference between basic and extended regular expressions is in the behavior of a few special characters: ‘?’, ‘+’, parentheses, braces (‘{}’), and ‘|’.

With basic (BRE) syntax, these characters do not have special meaning unless prefixed with a backslash (‘\’); While with extended (ERE) syntax it is reversed: these characters are special unless they are prefixed with backslash (‘\’).

source: https://www.gnu.org/software/sed/manual/sed.html#BRE-vs-ERE

具体实践:

sed -E 's#(http.*github[^/]*/)#https://github.crazypeace.workers.dev/\1#g' 

sed -e 's#\(http.*github[^/]*/\)#https://github.crazypeace.workers.dev/\1#g' 

等同.

我个人比较喜欢 -E 的写法,( 不需要用 \ 来转义。


评论

  1. 为了减少记忆的精力, 直接记住用 -r 参数 (前面不需要\

    回复删除

发表评论

The Hot3 in Last 7 Days

用 Cloudflare worker 抓取 喷嚏网 浮世汇 生成 RSS

白嫖worker 自建短链服务 Url-Shorten-Worker 变身文件保管站 File Storage 保存到 R2对象存储 搭建手册

KV-woker 纯练手项目 保护 _ 开头的 Key VS code 重构 JS 代码 提取代码为函数