SHARE
TWEET

re.sub example

DeaD_EyE May 23rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2.  
  3.  
  4. def regex_replace(string, replace):
  5.     regex = re.compile(r'(?P<start><!-- start Menu -->)(?P<middle>[\w\d\s]+)(?P<end><!-- end Menu -->)')
  6.     output = r'\1' + replace + r'\3'
  7.     return regex.sub(output, string)
  8.  
  9. regex_replace('<!-- start Menu -->  und  <!-- end Menu -->', 'Hello World')
  10. # '<!-- start Menu -->Hello World<!-- end Menu -->'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top