Advertisement
DeaD_EyE

re.sub example

May 23rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  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 -->'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement