re.split(pattern, string, maxsplit=0, flags=0)
The re.split function splits the text string, considering as separator the occurrences of the regular expression pattern.
- 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.
The re.split function returns a list made up of text strings.
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:
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:
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.