For marketers
who love technology
Home » » Using regexp (regular expressions) in python

# Using regexp (regular expressions) in python

Regular expressions are really useful whenever you want to search for or replace text in a file or a string. That means, all the time! They are concise, elegant, powerful and simple. So let's have a look to some examples.

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.