sed给出的前面的正则表达式无效

Spa*_*196 13 regex bash command-line sed

我一直在处理一个sed脚本文件,当我运行它时,我遇到了"Invalid Preceding regular expression"错误.以下是整个文件.

我已经在这个网站和其他地方进行了很多搜索.这里提出的许多问题都导致需要扩展正则表达式,而不正确地转义.我已将此定义为扩展表达式,因为它是电子邮件替换所需的.

#!/bin/sed -rf
#/find_this thing/{
#s/ search_for_this/ replace_with_this/
#s/ search_for_this_other_thing/ replace_with_this_other_thing/
#}

#Search and replace #ServerAdmin (with preceding no space) email addresses using a regular expression that has the .com .net and so on domain endings as option so it will find root@localhost and replace it in line with admin's email address.

ServerAdmin/ { 
s/\b[A-Za-z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
}
#Enable user's Public HTML directories
/UserDir/ {
s/disable$/enable/ 
s/^#User/User/
}
#Replace the only #ServerName (with preceding no space) followed space and text with Our server ip
/#ServerName */ c\ ServerName server.ip.address.here/
Run Code Online (Sandbox Code Playgroud)

我将它从termal称为./config-apache.sed /etc/httpd/conf/httpd.conf并将其返回.

/bin/sed: file ./apache-install.sed line 12: Invalid preceding regular expression
Run Code Online (Sandbox Code Playgroud)

在vim line 12的内部我被识别为}上面的单个#Enable user's Public HTML directories

Jon*_*ler 17

似乎GNU sed不喜欢PCRE非捕获表示法:

...(?:...)...
Run Code Online (Sandbox Code Playgroud)

尝试:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+(\.[A-Za-z]]{2,4})?\b/email@example.com/
Run Code Online (Sandbox Code Playgroud)

GNU sed似乎没问题.但是,你还有一些工作要做.给定下面的第一行作为输入,输出是第二行:

abc def@ghi.jk aaa
abc email@example.comjk aaa
Run Code Online (Sandbox Code Playgroud)

给出这个结果有两个问题:

  1. 本]]应该是一个单一的].
  2. 您正在寻找前一个正则表达式中的尾随点,因此您不希望在域后缀的最后部分使用一个.

这样做的工作:

s/\b[A-Za-z0-9._%-]+@([a-zA-Z0-9-]+\.)+([A-Za-z]{2,4})?\b/email@example.com/

abc def@ghi.jk aaa
abc email@example.com aaa
Run Code Online (Sandbox Code Playgroud)

  • 另一个长达一小时的头痛被规避了.我喜欢这个网站. (6认同)