简体   繁体   中英

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

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

I am getting error Unmatched [ in regex marked by <---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. That is what (?=...) does. It looks for a point after which there is a </p> and substitutes it with the replacement string.

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> . The si options will enable case-insensitive matching, and . will also match newline symbols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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