What:

Regex stands for  Regular expression. It is a sequence of characters that forms some pattern. These patterns can be used to search or match strings. Python has 're' module for applying regex on strings. Regex is build up of some special characters and alphabets.

Why:

Regex is useful, when we have to do parsing on a lot of data. A simple character can match a whole string; sounds already fascinating. The statement will become more clear after the article. 

How:

The Python re module has some functions, that can be used for pattern matching and searching using regex:
First of all, on the top of your python script do this:
import re 
set some variables:
pat = r'[\'\"][\w][^\'\"].*jpe?g[\'\"]'
text = '"http://www.google.image1.jpeg"'

1. re.match

        This function is used to match some pattern at the beginning of a string. It returns a match object. Which contains many attributes, we can see them using, dir method. The attribute group method used in following example displays the string matched by pattern, grouping is discussed in the post later.

example:
match = re.match(pat,text)
print dir(match)
print match.group()
Output:
['__class__', '__copy__', '__deepcopy__',  '__delattr__', '__doc__', '__format__',  '__getattribute__', '__hash__', '__init__',  '__new__', '__reduce__', '__reduce_ex__',  '__repr__', '__setattr__', '__sizeof__',  '__str__', '__subclasshook__','end',  'endpos', 'expand', 'group', 'groupdict',  'groups', 'lastgroup', 'lastindex', 'pos',  're', 'regs', 'span', 'start', 'string']'

"http://www.google.image1.jpeg"'

2.re.search:

        Similar to match, but differ in the manner that, it searches throughout the string and not just beginning.
example:
text2 = 'href="http://www.google.image1.jpeg"'
search = re.search(pat,text2)
print search.group()
Output:
'"http://www.google.image1.jpeg"'

Difference between match and search:
Try this:
example:
text2 = 'href="http://www.google.image1.jpeg"'
match=re.match(pat,text2)
print match
Output:
None
match and search both return an object instance. So we use group method to print the matched string.

3. re.findall:

        It finds all the occurences of the pattern and returns a list of them.
example:
text='href="http://www.google.image3.gif"
      href="http://www.google.image1.jpeg"
      href="http://www.google.image2.jpg"
      href="http://www.google.image1.png"'
find=re.findall(pat,text)
print find
Output:
['"http://www.google.image1.jpeg"', '"http: //www.google.image2.jpg"']
Above functions are for matching patterns. 're' also have function for replacing text with another. 

4. re.sub:

        It finds the part of string which matches the pattern and replace it with a string provided in arguments as replacement.
example:
text='href="http://www.google.image3.gif"
      href="http://www.google.image1.jpeg"
      href="http: //www.google.image2.jpg"
      href="http://www.google.image1.png"'
pat=r'\.[\w]+\'\"]'
repl='.jpg'
print re.sub(pat,repl,text)
Output:
href="http://www.google.image3.jpg"
href="http://www.google.image1.jpg"
href="http: //www.google.image2.jpg"
href="http://www.google.image1.jpg"