Python Regex findall()

Summary: in this tutorial, you’ll learn how to use the Python regex findall() function to find all matches of a pattern in a string.

Introduction to the Python regex findall() function

The findall() is a built-in function in the re module that handles regular expressions. The findall() function has the following syntax:

re.findall(pattern, string, flags=0)Code language: Python (python)

In this syntax:

  • pattern is a regular expression that you want to match.
  • string is the input string
  • flags is one or more regular expression flags that modify the standard behavior of the pattern.

The findall() function scans the string from left to right and finds all the matches of the pattern in the string.

The result of the findall() function depends on the pattern:

  • If the pattern has no capturing groups, the findall() function returns a list of strings that match the whole pattern.
  • If the pattern has one capturing group, the findall() function returns a list of strings that match the group.
  • If the pattern has multiple capturing groups, the findall() function returns the tuples of strings that match the groups.

It’s important to note that the non-capturing groups do not affect the form of the return result.

Python regex findall() function examples

Let’s take some examples of using the findall() function.

1) Using the Python regex findall() to get a list of matched strings

The following example uses the findall() function to get a list of color names that start with the literal string bl:

import re s = "black, blue and brown" pattern = r'bl\w+' matches = re.findall(pattern,s) print(matches)Code language: Python (python)

Output:

['black', 'blue']Code language: Python (python)

The following pattern matches a literal string bl followed by one or more word characters specified by the \w+ rule:

'bl\w+'Code language: Python (python)

Therefore, the findall() function returns a list of strings that match the whole pattern.

2) Using the findall() function with a pattern that has a single group

The following example uses the findall() function to get a list of strings that match a group:

import re s = "black, blue and brown" pattern = r'bl(\w+)' matches = re.findall(pattern,s) print(matches)Code language: Python (python)

Output:

['ack', 'ue']Code language: Python (python)

This example uses the regular expression r'bl(\w+)' that has one capturing group (\w+). Therefore, the findall() function returns a list of strings that match the group.

3) Using the findall() function with a pattern that has multiple groups

The following example uses the findall() functions to get tuples of strings that match the groups in the pattern:

import re s = "black, blue and brown" pattern = r'(bl(\w+))' matches = re.findall(pattern,s) print(matches)Code language: Python (python)

Output:

[('black', 'ack'), ('blue', 'ue')]Code language: Python (python)

In this example, the pattern r'(bl(\w+))' has two capturing groups:

  • (\w+) captures one or more word characters.
  • (bl(\w+)) captures the whole match.

4) Using the findall() function with a regular expression flag

The following example uses the findall() function with the re.IGNORECASE flag:

import re s = "Black, blue and brown" pattern = r'(bl(\w+))' matches = re.findall(pattern, s, re.IGNORECASE) print(matches)Code language: Python (python)

Output:

[('Black', 'ack'), ('blue', 'ue')]Code language: Python (python)

In this example, we use the re.IGNORECASE flag in the findall() function that ignores the character cases of the matched strings. Therefore, the output includes both Black and blue.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *