Skip to content Skip to sidebar Skip to footer

Pattern Matching And Replacing In Python

I'm trying to take a string that can be anything like 'Hello here is a [URL]www.url.com[/URL] and it's great.' and be able to extract whatever is between [URL] and [/URL] and then

Solution 1:

Use regular expressions:

import re
print re.sub(r'\[URL\](.*?)\[/URL\]',r'<a href="\1">\1</a>', "Hello here is a [URL]www.url.com[/URL] and it's great.")

Post a Comment for "Pattern Matching And Replacing In Python"