Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将短划线替换为锚文本中的空格_Regex_Vim_Sed - Fatal编程技术网

Regex 将短划线替换为锚文本中的空格

Regex 将短划线替换为锚文本中的空格,regex,vim,sed,Regex,Vim,Sed,我只想在锚定HTML代码中用空格替换破折号,如下所示: <a href="https://example.com/hello-world-hi">hello-world-hi</a> 替换后,将是: <a href="https://example.com/hello-world-hi">hello world hi</a> 如何告诉正则表达式只替换锚文本中的破折号 不应该尝试用正则表达式解析HTML,而应该使用解析器 对于命令行处理,

我只想在锚定HTML代码中用空格替换破折号,如下所示:

<a href="https://example.com/hello-world-hi">hello-world-hi</a>

替换后,将是:

<a href="https://example.com/hello-world-hi">hello world hi</a>


如何告诉正则表达式只替换锚文本中的破折号

不应该尝试用正则表达式解析HTML,而应该使用解析器

对于命令行处理,有(为许多Linux发行版打包的)及其
hxpipe
hxunpipe
命令,它们可以将HTML转换为一种易于使用基于行的工具处理的格式,然后返回:

$ echo '<a href="https://example.com/hello-world-hi">hello-world-hi</a>' | hxpipe
Ahref CDATA https://example.com/hello-world-hi
(a
-hello-world-hi
)a
-\n
sed命令的可读性和注释性更好:

sed '
    /^(a$/,/^)a$)/{   # If we are within an anchor tag...
        /^-/s/-/ /2g  # If the line starts with "-" (text), replace all but the
    }                 #   first hyphen with a space
'
hxpipe
通过以
-
开头的行指示文本,因此我们替换除该连字符外的所有连字符。
s///2g
的行为是特定于GNU的,可能对其他sed的工作方式有所不同

最后,我们将其“取消管道”回HTML:

$ echo '<a href="https://example.com/hello-world-hi">hello-world-hi</a>' |
> hxpipe |
> sed '/^(a$/,/^)a$)/{/^-/s/-/ /2g}' |
> hxunpipe
<a href="https://example.com/hello-world hi">hello world hi</a>
$echo''|
>hxpipe|
>sed'/^(a$/,//^)a$)/{/^-/s/-//2g}|
>hxunpipe

您不应该尝试用正则表达式解析HTML,而应该使用解析器

对于命令行处理,有(为许多Linux发行版打包的)及其
hxpipe
hxunpipe
命令,它们可以将HTML转换为一种易于使用基于行的工具处理的格式,然后返回:

$ echo '<a href="https://example.com/hello-world-hi">hello-world-hi</a>' | hxpipe
Ahref CDATA https://example.com/hello-world-hi
(a
-hello-world-hi
)a
-\n
sed命令的可读性和注释性更好:

sed '
    /^(a$/,/^)a$)/{   # If we are within an anchor tag...
        /^-/s/-/ /2g  # If the line starts with "-" (text), replace all but the
    }                 #   first hyphen with a space
'
hxpipe
通过以
-
开头的行指示文本,因此我们替换除该连字符外的所有连字符。
s///2g
的行为是特定于GNU的,可能对其他sed的工作方式有所不同

最后,我们将其“取消管道”回HTML:

$ echo '<a href="https://example.com/hello-world-hi">hello-world-hi</a>' |
> hxpipe |
> sed '/^(a$/,/^)a$)/{/^-/s/-/ /2g}' |
> hxunpipe
<a href="https://example.com/hello-world hi">hello world hi</a>
$echo''|
>hxpipe|
>sed'/^(a$/,//^)a$)/{/^-/s/-//2g}|
>hxunpipe
  • 直观地选择该标记的内容:

    vit
    
  • 对视觉选择覆盖的文本执行替换:

    :s/\%V-\%V/ /g
    
  • 直观地选择该标记的内容:

    vit
    
  • 对视觉选择覆盖的文本执行替换:

    :s/\%V-\%V/ /g
    

  • 您仍然可以使用替换来执行此操作:

    :%s:\(<a [^>]*>\)\(.\{-}\)\(</a>\):\=join([submatch(1),substitute(submatch(2),'-',' ','g'),submatch(3)],''):g
    
    :%s:\(]*>\)\(.\{-}\(\):\=加入([子匹配(1),替换(子匹配(2),'-','','','',子匹配(3)],'':g
    
    您仍然可以使用替换:

    :%s:\(<a [^>]*>\)\(.\{-}\)\(</a>\):\=join([submatch(1),substitute(submatch(2),'-',' ','g'),submatch(3)],''):g
    
    :%s:\(]*>\)\(.\{-}\(\):\=加入([子匹配(1),替换(子匹配(2),'-','','','',子匹配(3)],'':g
    
    Hmmm。我基本上同意应该使用HTML解析器(我没有投票)。但是这个hxpipe看起来非常粗糙,尤其是与sed结合使用时。在我看来,一个小Python(或其他什么)程序会更干净。感谢您为HTML解析引入这个util。瑞士刀和sed结合在一起。@hek2mgl我想sed看起来总是很粗糙;)但我刚刚意识到我可以稍微清理一下(不需要循环)。是的,Python/Perl或类似的工具会更简洁,但是如果你真的想要一个可管道化的工具,我发现
    hxpipe
    hxunpipe
    对于快速原型和一次性脚本来说是非常可行的。嗯,也许我应该有一天尝试一下。以前从未听说过html xml UTIL。谢谢你。我基本上同意应该使用HTML解析器(我没有投票)。但是这个hxpipe看起来非常粗糙,尤其是与sed结合使用时。在我看来,一个小Python(或其他什么)程序会更干净。感谢您为HTML解析引入这个util。瑞士刀和sed结合在一起。@hek2mgl我想sed看起来总是很粗糙;)但我刚刚意识到我可以稍微清理一下(不需要循环)。是的,Python/Perl或类似的工具会更简洁,但是如果你真的想要一个可管道化的工具,我发现
    hxpipe
    hxunpipe
    对于快速原型和一次性脚本来说是非常可行的。嗯,也许我应该有一天尝试一下。以前从未听说过html xml UTIL。谢谢你。对于perl,
    :%perldo s/^]*>(*SKIP)(*F)|-//g
    不确定它对于html标记有多强大……对于perl,
    :%perldo s/^]*>(*SKIP)(*F)|-//g
    不确定它对于html标记有多强大……这一点都不实用:P