Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: preg_replace function analog

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 i did run this on python? re.sub?

like image 997
Roman Nazarkin Avatar asked Mar 11 '13 05:03

Roman Nazarkin


1 Answers

As @bereal commented use the Regular Expression module re.sub.

Here's a simple example

Python:

>>> import re
>>> re.sub(r'([^A-Z])([A-Z])', r'\1_\2', 'camelCase').lower()
'camel_case'

And just for kicks here's it on PHP too:

<?php
echo strtolower(preg_replace('/([^A-Z])([A-Z])/', '$1_$2', 'camelCase'));
// prints camel_case
like image 68
icc97 Avatar answered Oct 02 '22 16:10

icc97