views:

136

answers:

2

I would like to use UltraEdit regular expression (perl) to replace the following text with some other text in a bunch of html files:

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

+3  A: 

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.

Amarghosh
+2  A: 

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


*Or at least it would if UltraEdit's regex engine didn't still have that bug making greedy quantifiers non-greedy if the regex engine has to cross newlines for a greedy match. Currently (V16.00.0.1040) the regex (?s)XX.*YY will match only up to the first YY in the following text:

XX
Blah
YY (this is the end of the match)
More Blah
YY (this is where the greedy match should really end)
Tim Pietzcker