Anchors and Boundaries
In regular expressions, anchors and boundaries are used to match patterns at specific locations in the input string.
- The
^
anchor is used to match the beginning of the line. For example, the pattern'^Hello'
will match any line that starts with 'Hello'. - The
$
anchor is used to match the end of the line. For example, the pattern'World$'
will match any line that ends with 'World'. - The
\b
word boundary is used to match the position between a word character and a non-word character. For example, the pattern'\bHello'
will match the word 'Hello' only at the beginning of the string.
Anchors and boundaries are useful when you want to match patterns that have specific positioning requirements in the input string. They allow you to perform more precise matching and avoid false positives.
Here's an example of using the \b
word boundary to match 'Hello' only at the beginning of the string:
{{< code-block "python" >}} import re
test_string = 'Hello World'
Using \b to match 'Hello' only at the beginning of the string
pattern = r'\bHello' match = re.search(pattern, test_string)
Print the result
print(match) {{< /code-block >}}
In this example, the \b
word boundary is used to match the word 'Hello' only at the beginning of the string. The re.search()
function is used to find a match, and it returns a match object if there is a match.
You can experiment with anchors and boundaries in your regular expressions to achieve more precise matches in your text.
xxxxxxxxxx
import re
test_string = 'Hello World'
# Using \b to match 'Hello' only at the beginning of the string
pattern = r'\bHello'
match = re.search(pattern, test_string)
# Print the result
print(match)