]*?>.*?

Python: preg_replace analog function

I have a litle expression in PHP:

$search = array("'<(script|noscript|style|noindex)[^>]*?>.*?</(script|noscript|style|noindex)>'si", "'<\!--.*?-->'si", "'<[\/\!]*?[^<>]*?>'si", "'([\r\n])[\s]+'"); $replace = array ("", "", " ", "\\1 "); $text = preg_replace($search, $replace, $this->pageHtml); 

How did i do this in python? re.sub ?

+4
source share
1 answer

As @bereal commented , use the re.sub regex re.sub .

Here is a simple example.

PHP:

 <?php echo strtolower(preg_replace('/([^AZ])([AZ])/', '$1_$2', 'camelCase')); // prints camel_case 

Python:

 >>> import re >>> re.sub(r'([^AZ])([AZ])', r'\1_\2', 'camelCase').lower() 'camel_case' 
+3
source

All Articles