简体   繁体   中英

How to match a newline \n in a perl regex?

I want to match this line,

<center>'''<font color="blue"><font size="18.0pt">No Change Alarms Help &amp; Information</font></font>'''</center>

and replace it with,

=<center>'''<font color="blue">No Change Alarms Help &amp; Information</font>'''</center>=

Now it would be simple if the tags were always font colour or center, but they can be absolutely anything and there can be multiple of them.

My current code is this:

$html =~ s/<font size=".+">(.+)<\/font>/$1/g;

but this obviously does not do the = on each end.

What I would like to do is this:

$html =~ s/\n(.+)<font size=".+">(.+)<\/font>(.+)\n/=$1$2$3=/g;

However it fails to match the newline characters and I cannot figure out how to make it match them, any clues?

(I'm converting html to wiki markup, however the converter stuffs up the font sizes so I'm manually converting them to wiki style headings.)

在我的查询结束时,我需要的只是/gm ,默认情况下它忽略了新的行。

$string_given =~ s/matching expression/sustitution/s;

i think this will work,using the /s modifier, which mnemonically means to "treat string as a single line". This changes the behaviour of "." to match newline characters as well.

In order to match the beginning of this comment to the end, we add the /s modifier like this:

$str =~ s/<!-- Start.*End of section -->//s;

Without the /s, it wouldn't match at all.

In some cases it might not work because of how perl "slurps" the input. Passing -0777 as a parameter will make it consider multiple lines. (Pass it along with your other parameters, eg perl -0777pi -e )

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