Python re.findall() Method Tutorial

Regular Expression or re module is used to search given text data according to the provided patterns. Python provides the regular expression functions via the re module. The re module is a built-in module and there is no need to install it. The findall() method is provided by the re module and used to math all regular expressions for a given string or text. In order to use the regular expressions in Python the module re should be imported like below.

import re

re.findall() Method Syntax

The re.findall() method has the following syntax where both of the REGEX and STRING parameters are required.

re.findall(REGEX , STRING)
  • REGEX is the regex pattern which will be applied to the STRING and the result matches will be returned. This parameter is required.
  • STRING is the string or text which will be searched for the specified REGEX pattern. This parameter is required.

Regex Characters and Patterns

Before starting examples with the re.findall() regex matching examples lets list some popular and useful regex characters and patterns with the findall() method.

REGEXDESCRIPTION
[]Represent special character class
^The begging of the line
$The end of the line
.Any single character
?Zero or more occurrence for the specified character
|Or
*Any number of occurrences
+One or more occurences
{}Group a regex
()Enclose a group of regex

re.findall() Examples

In the following example we will use to find and match alphabet and numbers with the specified regex. Then provide it to the re.findall() method which will return all matched lines as a list.

import re

re_alphabet= "python.*"

re_number= "\d+"

text = """ I like the web site pythontect.com
           This site is the best site of the 2020"""

match = re.findall(re_alphabet , text)
print(match)

match = re.findall(re_number , text)
print(match)

The output will be like below. As we can see as the findall() method may return zero, single or more matches the resulted matches will be provided as lists. Below we can see that every math is provided as a list item.

['pythontect.com']

['2020']

Find All IP Addresses

The regex findall() method can be used to match and find all IP addresses in a text file. The IP address regex can be expressed with the (?:[0-9]{1,3}.){3}[0-9]{1,3} pattern.

import re

all_ip_addresses = re.findall("(?:[0-9]{1,3}\.){3}[0-9]{1,3}" , text)

for ip_address in all_ip_addresses:
   print(ip_address)

Find All MAC Addresses

MAC address is used by ethernet cards, wireless networks for Layer2 communication. The method findall() can be used to match and find all mac addresses in a given text. We assume that the MAC addresses are provided in format AA-BB-CC-DD-EE-FF .

import re

all_ip_addresses = re.findall("([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})" , text)

for ip_address in all_ip_addresses:
   print(ip_address)

re.findall() vs re.search()

The re.search() is another method that can be used to make a regex-based search and match. It is very similar to the re.findall() method but there are some differences as we expect. The re.findall() method searches the whole text and returns all matches as a list but the re.search() method returns only the first match. So re.search() can be used for a simple test to check given regex.

import re

re_alphabet= "python.*"

re_number= "\d+"

text = """ I like the web site pythontect.com
           This site is the best site of the 2020"""

match = re.search(re_alphabet , text)
print(match)

The output is like below where there is a single match which span between 21 and 35 characters.

Leave a Comment