Skip to content

Regular Expressions

In Python a regular expression search is typically written as: match =, str)

The method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.

The code match =, str) stores the search result in a variable named "match".


The 'r' at the start of the pattern string designates a python "raw" string which passes through backslashes without change which is very handy for regular expressions. It is always better to write pattern strings with the 'r' just as a habit.

Escape characters in Python use the backslash (\). The string value '\n' represents a single newline character, not a backslash followed by a lowercase n. You need to enter the escape character \ to print a single backslash. So '\n' is the string that represents a backslash followed by a lowercase n. However, by putting an r before the first quote of the string value, you can mark the string as a raw string, which does not escape characters.

Since regular expressions frequently use backslashes in them, it is convenient to pass raw strings to the function instead of typing extra backslashes. Typing r'\d\d\d-\d\d\d-\d\d\d\d' is much easier than typing '\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d'.

Basic Patterns

The power of regular expressions is that they can specify patterns, not just fixed characters. Here are the most basic patterns which match single chars:

  • a, X, 9, < -- ordinary characters just match themselves exactly. The meta-characters which do not match themselves because they have special meanings are: . ^ $ * + ? { [ ] \ | ( ) (details below)

  • . (a period) -- matches any single character except newline '\n'

  • \+ -- match one or more occurrences of the pattern to its left, e.g. 'i+' = one or more i's
  • \* -- match one or more occurrences of the pattern to its left
  • ? -- match 0 or 1 occurrences of the pattern to its left
  • \w -- (lowercase w) matches a "word" character: a letter or digit or underbar [a-zA-Z0-9_]. Note that although "word" is the mnemonic for this, it only matches a single word char, not a whole word. \W (upper case W) matches any non-word character.
  • \b -- boundary between word and non-word
  • \s -- (lowercase s) matches a single whitespace character -- space, newline, return, tab, form [\n\r\t\f]. \S (upper case S) matches any non-whitespace character.
  • \t, \n, \r -- tab, newline, return
  • \d -- decimal digit [0-9] (some older regex utilities do not support but \d, but they all support \w and \s)
  • ^ = start, $ = end -- match the start or end of the string
  • \ -- inhibit the "specialness" of a character. So, for example, use \. to match a period or \\ to match a slash. If you are unsure if a character has special meaning, such as '@', you can put a slash in front of it, \@, to make sure it is treated just as a character.
>>> import re
>>> match ='\d\d\d-\d\d\d-\d\d\d\d','My number is 415-555-4242.')

Adding parentheses will create groups in the regex: (\d\d\d)-(\d\d\d-\d\d\d\d). Then you can use the group() match object method to grab the matching text from just one group.

>>> match ='(\d\d\d)-(\d\d\d-\d\d\d\d)','My number is 415-555-4242.')

Match Multiple Groups with the Pipe (r'Last|First') will match either Last or First match ='First|Last','My First name is Jon')

match ='First|Last','My Last name is Dam')

By using the pipe character and grouping parentheses, you can specify several alternative patterns you would like your regex to match.

>>> mo ='Bat(man|mobile|copter|bat)','Batmobile lost a wheel')

To match a pattern optionally, that is to find a match if that bit of text is there or not, use ? match ='Bat(wo)?man','The Adventures of Batman')

match ='Bat(wo)?man','The Adventures of Batwoman')

>>> match ='jeev?a', 'jeeva')
>>> match ='jeev?a', 'jeea')

Matching Zero or More with the Star

The * (called the star or asterisk) means “match zero or more”

>>> match ='Bat(wo)*man','The Adventures of Batman')
>>> match ='Bat(wo)*man','The Adventures of Batwowowowoman')
>>> match ='jeev*a', 'jeevvvvvvvvvvvvvvvvvvvvva')
>>> match ='\d\s*\d\s*\d', 'xx123xx’) #digit+ 0 or more space +digit + 0 or more space + digit

Matching One or More with the Plus

While * means “match zero or more,” the + (or plus) means “match one or more.”

>>> match ='Bat(wo)+man','The Adventures of Batwoman')
>>> match ='Bat(wo)+man','The Adventures of Batwoman')
>>> match ='Bat(wo)+man','The Adventures of Batwowowowoman')
>>> match ='Bat(wo)+man','The Adventures of Batman')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

Matching Specific Repetitions with Curly Brackets

If you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in curly brackets. For example, the regex (Ha){3} will match the string 'HaHaHa', but it will not match 'HaHa', since the latter has only two repeats of the (Ha) group.

Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between the curly brackets. For example, the regex (Ha){3,5} will match 'HaHaHa', 'HaHaHaHa', and 'HaHaHaHaHa'.

You can also leave out the first or second number in the curly brackets to leave the minimum or maximum unbounded. For example, (Ha){3,} will match three or more instances of the (Ha) group, while (Ha){,5} will match zero to five instances. Curly brackets can help make your regular expressions shorter. These two regular expressions match identical patterns:



(Ha) {,2)
()|(Ha)|((Ha) (Ha))

Greedy and Nongreedy Matching

Python’s regular expressions are greedy by default, which means that in ambiguous situations they will match the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark.

>>> match ='(Ha){3,5}','HaHaHaHaHa')
>>> match ='(Ha){3,5}?','HaHaHaHaHa')

The findall() Method

In addition to the search() method, Regex objects also have a findall() method. While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of every match in the searched string.

>>> match ='\d\d\d-\d\d\d-\d\d\d\d','Cell: 415-555-9999 Work: 212-555-0000')
>>> re.findall(r'\d\d\d-\d\d\d-\d\d\d\d','Cell: 415-555-9999 Work: 212-555-0000')
['415-555-9999', '212-555-0000']

Sample email matching:

email = '''\w+@\w',email) ==> 'doe@x''\w+@\w+',email) ==> 'doe@xyz''[\w-]+@\w+',email) ==> 'john-doe@xyz''[\w-]+@[\w.]+',email) ==> ''
>>> match  ='([\w-])+@([\w.]+)',email)

findall With Files

For files, you may be in the habit of writing a loop to iterate over the lines of the file, and you could then call findall() on each line. Instead, let findall() do the iteration for you -- much better! Just feed the whole file text into findall() and let it return a list of all the matches in a single step (recall that returns the whole text of a file in a single string):

f = open('test.txt', 'r')
# Feed the file text into findall(); it returns a list of all the found strings
strings = re.findall(r'some pattern',

Substitution (optional)

The re.sub(pat, replacement, str) function searches for all the instances of pattern in the given string, and replaces them. The replacement string can include '\1', '\2' which refer to the text from group(1), group(2), and so on from the original matching text.

Here's an example which searches for all the email addresses, and changes them to keep the user (\1) but have as the host.

str = 'purple, blah monkey blah dishwasher'
## re.sub(pat, replacement, str) -- returns new string with all replacements,
## \1 is group(1), \2 group(2) in the replacement
print re.sub(r'([\w\.-]+)@([\w\.-]+)', r'\', str)
## purple, blah monkey blah dishwasher

To remove multiple space characters:

>>> a = '  This is a     test       line        '
>>> print re.sub('\s+',' ',a)
 This is a test line

Using space itself instead of /s

>>> re.sub(' +',' ', a)
' This is a test line '