Python: Regular Expressions

Regular Expressions are used to parse text. In the world of Big Data, being able to work with unstructured text is a big advantage.

To use regular expressions, first you must import the module. This is done by placing the command import re at the top of your code.

Now, let us examine this code below:

We want to see if dog (assigned to x) is in the sentence ‘I just a saw dog. He was chasing a cat.'(assigned to y)

Using the search() method from re, we ask if,y). Note you place the item you are searching by first in the parenthesis. returns a boolean value (True, False).

You can use with lists of search items as well.

Here z is taking one item from the list x at a time and running it through Notice ‘one’ returns True, while ‘two’ returns false.

Try your code in our online Python console:  


re.findall returns all instances of your search term. Notice it found water whether it was a stand alone word, or part of a larger word.



The re.split() method does pretty much what you would think it does. You can pick a delimiter and the method will split your string at that delimiter.

In the example below, ‘;‘ is my delimiter. Notice how it split my string in two, plus removed the delimiter for me.


Try your code in our online Python console:  

use to find position

You can use to find the starting and ending position of a search item in a string



If you want to exclude characters, use the ^ between square brackets [].

This example excludes the letter s = [^s] and puts the remaining characters in a list

In the second example, I add + after the []. This keeps all the characters together.


This next example is a useful tool you will find yourself using in text mining. Here we use [^?!. ]+ to remove punctuation.


Try your code in our online Python console:  

If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT. 

Last Lesson: Generators

Next Lesson: kwargs and args

Back to Python Course: Course


Leave a Reply