Beginner Level
Intermediate Level
Advanced Level
Introduction
If you are working with strings in Python, then you most likely have encountered scenarios where you need to search for a particular pattern within a string. This is where regular expressions or regex come in handy. Regex Anchors are special characters that allows us to match patterns at specific positions in a string. They help us to identify specific locations within a string where a pattern might occur, like the beginning or end of a line. In this tutorial, we will cover the different types of Regex Anchors in Python and how they can be used to make our string processing tasks more efficient and precise.
Table of Contents :
- Introduction to Regex Anchors
- ^ Anchor
- $ Anchor
- \A Anchor
- \Z Anchor
- \b Anchor
Introduction to Regex Anchors :
- In regular expressions, anchors are used to specify the position of a match within a string.
- Anchors do not match any characters, but they match a position in the string.
- Let's explore the commonly used anchors in regular expressions.
- ^ Anchor
- $ Anchor
- \A Anchor
- \Z Anchor
- \b Anchor
The ^ Anchor :
- The
^
anchor matches the start of a line or string. - Here's an example of using the
^
anchor in a regular expression: - Code Sample :
import re
pattern = r"^Hello"
string = "Hello, world! How are you?"
result = re.findall(pattern, string)
print(result)
# The above example will match the start of the string and return any occurrences of the word "Hello".
The $ Anchor :
- The
$
anchor matches the end of a line or string. - Here's an example of using the
$
anchor in a regular expression: - Code Sample :
import re
pattern = r"world$"
string = "Hello, world! How are you?"
result = re.findall(pattern, string)
print(result)
# The above example will match the end of the string and return any occurrences of the word "world".
The \A Anchor :
- The
\A
anchor matches the start of a string. - It's similar to the
^
anchor, but it doesn't match the start of a line in a multi-line string. - Here's an example of using the
\A
anchor in a regular expression: - Code Sample :
import re
pattern = r"\AHello"
string = "Hello, world! How are you?"
result = re.findall(pattern, string)
print(result)
# The above example will match the start of the string and return any occurrences of the word "Hello".
The \Z Anchor :
- The
\Z
anchor matches the end of a string. - It's similar to the
$
anchor, but it doesn't match the end of a line in a multi-line string. - Here's an example of using the
\Z
anchor in a regular expression: - Code Sample :
import re
pattern = r"world\Z"
string = "Hello, world! How are you?"
result = re.findall(pattern, string)
print(result)
# The above example will match the end of the string and return any occurrences of the word "world".
The \b Anchor :
- The
\b
anchor matches the boundary between a word character and a non-word character. - It's often used to match whole words.
- Here's an example of using the
\b
anchor in a regular expression: - Code Sample :
import re
pattern = r"\bworld\b"
string = "Hello, world! How are you?"
result = re.findall(pattern, string)
print(result)
# The above example will match the whole word "world" and return it as a list.
Prev. Tutorial : Character sets
Next Tutorial : Word Boundary