I provide below a simple code example to show the usage of regular expressions in python.
import re
text= r"\begin{environment}"
left = r'\\begin'
right = r''
r=re.compile(left)
text=r.sub(right,text)print text
This code returns:
{environment}
Take care to the 'r' before the strings: they manage all the character escape subtleties for you (see http://docs.python.org/2/howto/regex.html for the explanations about the "backslash plague")
If you want to master regex, have a look to O'Reilly books, they are amazing.
No comments:
Post a Comment