简体   繁体   English

Perl 正则表达式匹配和替换

[英]Perl Regex match and replace

I try insert new line after < /a>我尝试在</a>之后插入新行

I tried this 
perl -pe '/<\/a> $&\n /g' teste.txt

Example例子

Source code
<a href="/link/">link</a><a href="/link2/">link2</a>

Output输出

<a href="/link/">link</a>
<a href="/link2/">link2</a>

您缺少执行正则表达式替换的s命令。

perl -pe 's/<\/a>/$&\n /g' teste.txt

A plain /xxx/ will just perform a match, you need s/xxx/yyy/ to perform a single replacement, and a s/xxx/yyy/g to perform multiple replacements.普通的/xxx/只会执行匹配,您需要s/xxx/yyy/执行单个替换,以及s/xxx/yyy/g执行多个替换。 You may also replace the / with another character such as |您也可以将/替换为另一个字符,例如| so you do not need to escape any characters, which makes it more readable.所以你不需要转义任何字符,这使得它更具可读性。

perl -pe 's|</a>|$&\n|g;' teste.txt

Have a look at perldoc perlretut for a gentle introduction to perl regular expressions.查看perldoc perlretut对 perl 正则表达式的简要介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM