How do we match any character, including a linear feed in a Perl regular expression?

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

<style type="text/css"> #some-id{} .some-class{} //many other css styles follow </style> 

I tried using <style type="text/css">.*</style> , but of course this would not match anything because the dot matches any character except the string. I would also like to match a line to a line, and the line can be either \r\n or \n .

What should a regular expression look like?

Many thanks to all of you.

+4
source share
2 answers

In UltraEdit, you need to add (?s) to your regular expression so that the dot matches the new line.

I. e., search

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

I also made the quantifier lazy ( .*? ), Because otherwise you would match everything from the first <style> to the last </style> in the whole file.

Also keep in mind that this is a broken solution because regular expressions cannot parse HTML reliably, if at all. In UltraEdit, all you have is a scripting language and a parser will be better, but if it works in your case, then fine. Just make sure you don't match more (or less) than you want (think //comment containing a </style> tag ).

+5
source

Usually a point . matches all characters other than new. Use the s modifier in the regular expression to force the dot to match all characters, including the newline.

+6
source

Source: https://habr.com/ru/post/1310348/


All Articles