开发者

How do we match any single character including line feed in Perl regular expression?

I would like to use UltraEdit regular expression (perl) to replace the following text with some other text in a bunch of html fil开发者_如何学JAVAes:

<style type="text/css">

#some-id{}

.some-class{}

//many other css styles follow

</style>

I tried to use <style type="text/css">.*</style> but of course it wouldn't match anything because the dot matches any character except line feed. I would like to match line feed as well and the line feed maybe either \r\n or \n.

How should the regular expression look like?

Many thanks to you all.


Normally dot . matches all characters other than new line. Use the s modifier in the regex to force dot to match all characters including new line.


In UltraEdit, you need to prepend (?s) to your regex to make the dot match newline.

I. e., search for

(?s)<style type="text/css">.*?</style>

I've also made the quantifier lazy (.*?) because otherwise you would match everything from the first <style> to the last </style> in your entire file.

Also be advised that this is a flaky solution because regular expressions can't parse HTML reliably if at all. In UltraEdit, that's all you have - a scripting language and a parser would be better, but if it works in your case, then great. Just make sure you don't match more (or less) than you wanted (think //comment containing a </style> tag).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜