Quantcast
Channel: Python - Replace part of regex? - Stack Overflow
Browsing all 2 articles
Browse latest View live

Answer by Adam Smith for Python - Replace part of regex?

Two ways to do this, either capture your pre- and post-fixes, or use lookbehinds and lookaheads.# captures = "Example {String}"replaced = re.sub(r'({).*?(})', r'\1a\2', s)# lookarounds = "Example...

View Article


Python - Replace part of regex?

I have the following piece of code:import res = "Example {String}"replaced = re.sub('{.*?}', 'a', s)print replacedWhich prints:Example aIs there a way to modify the regex so that it prints:Example...

View Article
Browsing all 2 articles
Browse latest View live