17

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?

3
  • 2
    Yes, re.sub. Did you try it? Commented Mar 11, 2013 at 5:40
  • @bereal ok, how did i set multiple patterns and replacements to re.sub? Commented Mar 11, 2013 at 5:49
  • 2
    It doesn't seem to have a special call for that, but why not using '|' as described e.g. here? Commented Mar 11, 2013 at 5:56

1 Answer 1

21

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.