【问题标题】:Vim Search Replace Regexp If this, not that, and not thatVim 搜索替换正则表达式 if this, not that, and not that
【发布时间】:2021-10-01 22:28:37
【问题描述】:

我有一个页面,我正在尝试使用正则表达式修复大量指向它的链接。

例如。有些东西已经被替换了,它们就像href="/websites/site.." 一样在那里,所以我要一个人离开。但是,有些被从绝对替换为相对,但是我得到的脚本认为我正在更改的文件位于它们所在的 web 根目录中。所以当我得到像 href="/file" 这样的匹配时,我想匹配。随便。这是我正在使用的正则表达式

%s#\v(href\="/)&((href\="/websites)@!|(href\="//)@!)#href="/websites/site/#g

但是,它会像这样改变一组条目

<a href="/websites/site/websites/site/dog.html"> 
<a href="/websites/site//">
<a href="doghouse.html">
<a href="/websites/site/doghouse.html">

到这里

<a href="/websites/site/href="/websites/site/websites/site/dog.html">
<a href="/websites/site/href="/websites/site//">
<a href="doghouse.html">
<a href="/websites/site/href="/websites/site/doghouse.html">

任何有关获得此搜索/替换 vim 正则表达式的帮助将不胜感激。

谢谢

【问题讨论】:

    标签: vim regexp-replace


    【解决方案1】:

    好的,来了! :%s/\vhref\="\/(\/|websites)@!/href="\/websites\/site

    说明: 您要更改的所有字符串都以href="/ 开头,因此将其作为文字包含在内似乎是正确的调用。

    然后,您要确保既不存在文字“网站”也不存在“/”,以便与 (\/|website)@! 一起使用,然后,您希望使用新添加的内容进行更改,因此需要其余部分.

    【讨论】:

      【解决方案2】:

      您可以使用:help :global 完成此操作。根据您的解释,我不完全确定您要做什么,但您可以使用此命令排除带有“网站”的行,如下所示::g!'href="/websites/site/'s/LESS_COMPLICATED_REGEX/RESULT/

      这里我使用:g! 对与'' 中的正则表达式不匹配的所有行执行命令(您可以像往常一样使用/ 而不是')。要执行的命令是通常的:s,但在这里您不必担心它会在具有“网站”字符串的行上运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2011-05-23
        • 1970-01-01
        相关资源
        最近更新 更多