Matching a Selection of Characters
Learn about the concept of matching a specific selection of characters and its implementation.
Overview
Let’s start with matching an arbitrary character. The period character, when used in a regular expression pattern, is a meta-character that stands for a set containing all characters. This will match any single character. Using a period in the string means you don’t care what the character is, just that there is a character there. Here is some example output from the matchy()
function:
pattern = 'hel.o world' matches at match = <re.Match object; span = (0, 11),match = 'hello world'>pattern = 'hel.o world' matches at match = <re.Match object; span = (0, 11),match = 'helpo world'>pattern = 'hel.o world' matches at match = <re.Match object; span = (0, 11),match = 'hel o world'>pattern = 'hel.o world' not found in text = 'helo world'
Notice how the last example does not match because there is no character at the period’s position in the pattern. We can’t match nothing without some extra features. We’ll get to the idea of optional characters later in this section.
The []
to specify the matching character sets
What if we only want a smaller set of characters to match? We can put a set of characters inside square brackets to match any one of those characters. So, if we encounter the string [abc]
in a regular expression pattern, this defines a set of alternatives to match one character in the string being searched; this one character will be in the set of characters. Note that the []
around the set are meta-characters; they enclose ...
Get hands-on with 1400+ tech skills courses.