Use regular-expression split to divide a string wherever a pattern matches. Unlike basic string splitting, the separator can describe multiple matching forms, and an optional maximum limits the number of splits. section 2.4.5 section 2.3.3
Split at each run of digits
import re parts = re.split(r"\d+", "a1b2c3d4e") print(parts) # ['a', 'b', 'c', 'd', 'e'] The pattern r"\d+" matches one or more digits, so every matching run becomes a division point. section 2.4.5
Choose the splitting approach according to the input:
- Use basic str.split when the data has one known separator, such as commas between task fields. section 2.3.3
- Use regular-expression split when the boundary is a pattern rather than one literal separator. section 2.4.5
- Set a maximum split count when only an initial portion of the string should be divided; compare split and rsplit when direction matters. section 2.3.5 section 2.3.4
Table 2.3 Common regular expression methods (view table figure)
| search: Returns a Match if a match is found anywhere in the string. |
||
| match: Returns a Match only if a match is found at the string’s beginning. |
||
| findall: Returns a list of strings that match the pattern. When the pattern has multiple groups, the item is a tuple. |
||
| finditer: Returns an iterator3 that yields the Match objects. |
||
| sub: Creates a string by replacing the matched with the replacement. |
Compare split with search, match, findall, finditer, and sub through representative regular-expression calls and return values. section 2.4.5










