...
/Repeating and Grouping Characters Patterns
Repeating and Grouping Characters Patterns
Learn about the repeating and grouping patterns of characters in Python.
We'll cover the following...
Repeating patterns of characters
With this information, we can match most strings of a known length, but most of the time, we don’t know how many characters to match inside a pattern. Regular expressions can take care of this, too. We can modify a pattern with a suffix character. When we think of a regular expression as a product, a repeating sequence is like raising to a power. This follows the pattern of: a*a*a*a == a**4
.
The asterisk (*
) character says that the previous pattern can be matched zero or more times. This probably sounds silly, but it’s one of the most useful repetition characters. Before we explore why, consider some silly examples to make sure we understand what it does:
import refrom typing import Pattern, Matchdef matchy(pattern: Pattern[str], text: str) -> None:if match := re.match(pattern, text):print(f"{pattern=!r} matches at {match=!r}")else:print(f"{pattern=!r} not found in {text=!r}")matchy(r'hel*o', 'hello')matchy(r'hel*o', 'heo')matchy(r'hel*o', 'helllllo')
So, the *
character in the pattern says that the previous pattern (the l
character) is optional, and if present, can be repeated as many times as possible to match the pattern. The rest of the characters (h
, e
, and o
) have to appear exactly once.
This gets more interesting if we combine the asterisk with patterns that match ...