re.split

Full name
re.split
Library
re
Syntax

re.split(pattern, string, maxsplit=0, flags=0)

Description

The re.split function splits the text string, considering as separator the occurrences of the regular expression pattern.

Parameters
  • pattern: Search pattern.
  • string: Text in which to search.
  • maxsplit: Maximum number of splits to extract. If there are more possible splits than specified, the undivided text is returned as a single item at the end of the list.
  • flags: Search modifiers.
Result

The re.split function returns a list made up of text strings.

Examples

We can divide a sentence into words considering as a separator all the groups of consecutive characters that are not found in the words of any language with the following code:

pattern = r"\W+"
text = 'my cat and your dog play in the backyard'
re.split(pattern, text)
['my', 'cat', 'and', 'your', 'dog', 'play', 'in', 'the', 'backyard']

Continuing with the same example, we can specify a maximum of, for example, 3 blocks of text to extract with the following code:

re.split(pattern, text, maxsplit = 3)
['my', 'cat', 'and', 'your dog play in the backyard']

As can be seen, the rest of the original text string that has not been split is returned as a single block at the end.

Submitted by admin on Tue, 05/18/2021 - 08:27