简体   繁体   English

Perl Regex替换段落标签

[英]Perl Regex Substitute paragraph tag

I am trying to use a regex to replace contents inside of paragragh tags with contents followed by br br tags 我正在尝试使用正则表达式将paragragh标记内的内容替换为br br标记后的内容

 s/^[<]p[>](.+)[<][/]p[>]/\1[<]br[>][<]br[>]/

I am getting error Unmatched [ in regex marked by <---HERE m/^[<]p>[<][ 我收到错误Unmatched [在以<--- HERE m / ^ [<] p> [<] [

I tried changing [/] to backslash/ but im not really sure how to go about this. 我尝试将[/]更改为反斜杠/,但我不确定如何解决这个问题。 Thank you =] 谢谢=]

I actually got it was 我实际上是

s/^<p>(.+)<\/p>/\1<br><br>/;

Try this: 尝试这个:

s#(?=</p>)#<br><br>#

Whenever dealing with a / , I like to use another separator. 每当处理/ ,我喜欢使用另一个分隔符。 Here I'm using # . 在这里,我使用#

Since, you just want to append <br><br> and not touch anything else in <p>..</p> , we can use a lookahead anchor. 因为,您只想添加<br><br>而不触摸<p>..</p> ,我们可以使用先行锚。 That is what (?=...) does. 那是(?=...)所做的。 It looks for a point after which there is a </p> and substitutes it with the replacement string. 它寻找一个点之后有一个</p>并将其替换为替换字符串。

Note that look-around expressions are zero-width ie they do not match any char. 请注意,环顾四周的表达式为零宽度,即它们与任何字符都不匹配。

You can use this version of your regex: 您可以使用此版本的正则表达式:

s/<p>(.+?)<\/p>/\1<br><br>/si

I removed string start anchor ^ , it is highly possible you do not need it. 我删除了字符串起始锚^ ,很有可能您不需要它。 .+? will make sure you will match the closest matching </p> . 将确保您将匹配最接近的匹配</p> The si options will enable case-insensitive matching, and . si选项将启用不区分大小写的匹配和. will also match newline symbols. 还将匹配换行符。

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

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