开发者

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

开发者 https://www.devze.com 2023-01-31 07:59 出处:网络
I want to match this line, <center>\'\'\'<font color=\"blue\"><font size=\"18.0pt\">No Change Alarms Help &amp; Information</font></font>\'\'\'</center>

I want to match this line,

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

an开发者_开发技巧d 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.)


All I needed was /gm on the end of my query, turns out it ignores new lines by default.


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, e.g. perl -0777pi -e)


$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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号