Metacharacter

To understand the analogy of Regular Expression, MetaCharacters is useful, important, and will be used in re module function.

Untitled

\ - Backslash

import re

s = 'My.Skill'

# wwithout using \\
match = re.search(r'.', s)
print(match)

# using \\
match = re.search(r'\\.', s)
print(match)
<re.Match object; span=(0, 1), match='M'>
<re.Match object; span=(2, 3), match='.'>

Backslash (\\) ensure that the character will not be treated with special treatment. It will be assumed as a method to run away from the meta character.

Example of Metacharacter

import re

s = 'My.Skill'

# wwithout using \\
match = re.search(r'.', s)
print(match)

# using \\
match = re.search(r'\\.', s)
print(match)
<re.Match object; span=(0, 1), match='M'>
<re.Match object; span=(2, 3), match='.'>

[] - Square Brackets

Square Brackets ([ ]) represents character class that consists of a group of character that want to be matched. For example, character of class [abc] will be matching with random a, b, or c.

We also can determine the range character by using - in the square bracket.

For instance: