gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:48:26 30 4
gpt4 key购买 nike

我正在并且一直在处理一个 sed 脚本文件,当我运行它时遇到了“无效的前导正则表达式”错误。以下是完整文件。

我已经在此站点和其他地方对此进行了大量搜索。这里提出的许多问题导致需要扩展正则表达式或错误地转义。我已经将其定义为扩展表达式,因为电子邮件替换需要它。

#!/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/

我从 termal 调用它作为 ./config-apache.sed/etc/httpd/conf/httpd.conf 并返回它。

/bin/sed: file ./apache-install.sed line 12: Invalid preceding regular expression

在 vim 第 12 行的内部,我被识别为 #Enable user's Public HTML directories

上方的单个

最佳答案

看来 GNU sed 不喜欢 PCRE 非捕获符号:

...(?:...)...

尝试:

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

GNU sed 似乎可以接受。但是,您还有一些工作要做。给定下面的第一行作为输入,输出是第二行:

abc def@ghi.jk aaa
abc email@example.comjk aaa

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

  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

关于regex - sed 给出的前面的正则表达式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929668/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com