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''
text=r.sub(right,text)print text

This code returns:


Take care to the ‘r’ before the strings: they manage all the character escape subtleties for you (see for the explanations about the “backslash plague”)

If you want to master regex, have a look to O’Reilly books, they are amazing.

Leave a Reply

Your email address will not be published. Required fields are marked *