Programming

Python Programming Language

Regexp using module re

Sample use case #1: re.findall

This is a first simple and useful example of regexp power. In this case we use to split a long string into a list of english words, including support for apostrophe words and rejecting any non word character (aka [a-zA-Z0-9]).

1
2
3
4
5
6
>>> import re
>>> pattern = '[a-zA-Z0-9_\']+'
>>> test_string = "word palabra don't 1 Word 34 bEach"
>>> words = re.findall(pattern, test_string)
>>> words
['word', 'palabra', "don't", '1', 'Word', '34', 'bEach']

References