Python re.findall() – Find in String using Regular Expression

Python supports a variety of inbuilt modules and functions to perform different tasks. The regex module is one of them that provides several functions to perform various kinds of tasks, such as the replacement of strings, finding strings, etc.

The “re” (acronym of regex) module provides a regex expression that contains the value of the string, which is used to match the given string. Python provides a “re.findall()” function that belongs to the regex module. It is used to get a list of strings that matches the specified regex pattern.

This article provides a comprehensive guide on Python’s re.findall() function and how to find strings using regular expressions.

A discussion of the following topics is provided in this article:

So let’s begin!

What is Python re.findall()?

The “re.findall()” function of the “re” module retrieves a list of strings that matches the specified regex pattern. This inbuilt function returns all non-overlapping matches of the string in the form of a “list” of strings. The return order of the matched string will start from left to right. The syntax of the “re.findall()” function is shown below:

re.findall(pattern, string, flags=0)

In the following syntax:

  • The “pattern” refers to the string value that needs to be found in the targeted string.
  • The “string” parameter refers to the input string value.
  • The “flags” parameter is used to modify the code’s behavior. One of the most used flags in the regular expression is  “IGNORE_CASE”.

Example 1: Finding String Pattern in the Given String

The following example shows how to use “re.findall()” in Python:

Code:

import re

input_string = 'itslinuxfoss'
string_pattern = 'its' result = re.findall(string_pattern, input_string)
print(result)
print(type(result))

In the following code:

  • The “re” module is imported into the program.
  • An “input_ string” is initialized with the value “itslinuxfoss” in the program.
  • The string pattern “its” that needs to be found in the given string is initialized in a variable.
  • The “re.findall()” takes the string pattern and input string as an argument and returns the finding of the string in the form of a list.

Output:

The output shows the finding of the string from the input string.

Example 2: Finding String Pattern From Alpha-Numeric String

In the following example, the input string contains the alphabet and the number. To find the alphabet-specific pattern from the given string, the regex expression “[a-z]+” is used. Using the example below, let’s understand:

Code:

import re

input_string = 'its123linux456foss'
string_pattern = '[a-z]+'
result = re.findall(string_pattern, input_string)
print(result)
print(type(result))

In the following code:

  • The “re” module is imported at the start of the program.
  • The input string containing numbers and alphabets is initialized in the program.
  • The string pattern “[a-z]+” indicates that all alphabet values will be found from the input string.
  • The “re.findall()” takes the string pattern and input string as an argument and returns a list of strings based on the specified pattern.

Output:

The output shows that the “findall()” function retrieves the list of alphabets.

Example 3: Finding String Pattern in the Given String Using Flags

In the example given below, the string pattern is matched and returned on output as a list of strings using the flag parameter.

Code:

import re

input_string = 'itslinuxfoss-PYTHON-GUIDE'
string_pattern = '[a-z]+'
result = re.findall(string_pattern, input_string, re.IGNORECASE)
print(result) print(type(result))

In the following code:

  • The “re” module is imported in the program.
  • The input string containing upper and lower case alphabets is initialized.
  • The string pattern “[a-z]+” indicates that all alphabet character values will be found from the given string.
  • The “re.findall()” takes a new parameter value named “flags’ ‘. Here in the parameter “re.IGNORECASE” flag is used in the program to read all upper and lower case characters.

Output:

The output shows that all upper and lower case alphabets have been returned as lists.

Example 4: Finding String Pattern in the Given String Using Metacharacters

The metacharacters can also be used within the re.findall() function. There are 14 metacharacters used that can be used in this function. Some of them are “\, [ ], ?, ^ ” etc. Every metacharacter has a specific purpose; for example, “?” is used for matching the zero or one occurrence, “+” is used for matching one or more than one occurrence, and so on. The following example uses the regular expression “d” with the metacharacter “+” to find the digit numbers from the given string.

Code:

import re

input_string = "Hello! 123456789 itslinuxfoss 987654321"
regex_pattern = '\d+'
result = re.findall(regex_pattern, input_string)
print(result)

In the following code:

  • The “metacharacter” symbol along with the expression “d” is used to get all the digits from the given string and returns as a list.
  • The “re.findall()” function takes the value of parameters “pattern” and “input string” and returns the matched value on output.

Output:

The output shows the list of digits that match the specified pattern.

That’s it from this Python blog post!

Conclusion

In Python, the “re.findall()” function of the “regex” module returns the non-overlapping pattern of the given string. The return value will be in the form of a list of strings. The “re.findall()” returns the matched list of substrings from the given string. The “re.findall()” function also takes flags parameters to handle case-insensitive characters. This article provides a complete guide on Python’s “re.findall()” function.