### Learning Python: Programming and Data Structures- Tutorial 13- Regular Expression Matching

We'll take a quick look at regular expressions in Python.

1. We will need to 'import re' first.
2. We will create a test string and in that test string, we will try to search for a word starting with the letter 'H'.

## Searching for a pattern in a string

Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
>>> import re
>>> testString1 = "Time to say Hello World"
>>> match_object = re.search(r"(H....)",testString1)
>>> print match_object.group(1)

Output:
Hello

## Searching for Multiple Instances of a Pattern in a String

Let us try to look for all the numbers which occur in this string: "This is a string with numbers in it: 1, 100, 2, 3, 4,99"
For finding all instances of a pattern in a string, we use 'findall'.

>>> testString2 = "This is a string with numbers in it: 1, 100, 2, 3, 4,99"
>>> numbersInTheString = re.findall(r"(?:^|\b)([0-9]+)(?:\b)",testString2)
>>> for matchedNumber in numbersInTheString: print matchedNumber

Output:
...
1
100
2
3
4
99

## Replacing a pattern in a String, with a Substring

This can be done using "sub".

>>> numbersInTheString = re.sub(r"(?:^|\b)s[a-z]+","word_starting_with_s",testString2)
>>> print numbersInTheString

Output:
This is a word_starting_with_s with numbers in it: 1, 100, 2, 3, 4,99

## Example: Detecting an Email Address in a chunk of text

>>> testString = "This is a string with an email@address.com embedded in it."
>>> matchObj = re.search(r"(?:\b)([^\s]*\@[^\s]*\.[a-zA-Z]{2,4})(?:\b)",testString)
>>> print matchObj.group(1)

Output:

## Example: Detecting the domain name in a given Url

>>> testString="http://www.thehindu.com/features/education/issues/engineering-students-locked-into-microsoft-office/article4640546.ece"
>>> matchObj = re.search(r"(?:https?\:\/\/)?([^\/]*)",testString)
>> print matchObj.group(1)

Output:
www.hindu.com