4

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
bobo
  • 8,439
  • 11
  • 57
  • 81

2 Answers2

6

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
  • 58,710
  • 11
  • 92
  • 121
5

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

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • @redDevil: The first sentence of my answer explains it: This modifier allows the dot to match newline characters (which it doesn't by default). See also Amarghosh's answer and the tutorial linked therein. – Tim Pietzcker Mar 24 '15 at 13:54