Interlude: Common tasks

Tasks like matching phone numbers, ip addresses, dates, etc are so common that you can often find them collected as a library. This chapter shows some examples for CommonRegex. re module documentation also has a section on tasks like docs.python: tokenizer. See also Awesome Regex: Collections.


You can either install commonregex as a module or go through and choose the regular expression you need. There are several ways to use the patterns, see CommonRegex: Usage for details.

>>> from commonregex import ip

>>> data = 'hello okay'
>>> ip.findall(data)

If you check ip.pattern, you'll find (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) repeated without any other restriction. So, this will not prevent partial matches. You'll have to add those conditions yourself.

>>> data = ' 567.12.2.1'

# wrong matches
>>> ip.findall(data)
['', '', '']

# corrected usage
>>> [e for e in data.split() if ip.fullmatch(e)]


Some patterns are quite complex and not easy to build and validate from scratch. Libraries like CommonRegex are helpful to reduce your time and effort needed for commonly known tasks. However, you do need to test the solution for your use case. See also stackoverflow: validating email addresses.