Nearby lessons
123 of 159Python - Regex Anchors and Flags
- Understand how the ^ anchor forces a pattern to match only at the beginning of a string
- Use the $ anchor to match a pattern only at the end of a string
- Combine ^ and $ to constrain a match to the complete string
- Apply re.IGNORECASE and its short form re.I for case-insensitive matching
- Distinguish between anchors and flags in a Regular Expression
Introduction to Regex Anchors and Flags
Regular Expressions provide special symbols and flags to control where a pattern should match and how matching should be performed.
In this part, we will study:
^ → Start of string
$ → End of string
re.IGNORECASE → Case-insensitive matching
These features are very useful when we want to check whether:
- A string starts with a particular pattern.
- A string ends with a particular pattern.
- A pattern should match without considering uppercase and lowercase differences.
The ^ Anchor - Start-of-String Matching
The caret symbol ^ is used to check whether a pattern occurs at the beginning of the string.
Simple Definition:
^ represents the beginning of the string.
Syntax:
^pattern
Example:
^Learning
This pattern means the string must start with Learning.
Consider the string:
Learning Python is very easy
Pattern:
^Learning
Python checks only the beginning:
Learning Python is very easy
^^^^^^^^
│
└── Pattern found at beginning
Therefore, the match is successful.
But consider:
Python Learning is very easy
Here, Learning exists in the string, but it is not at the beginning.
Python Learning is very easy
^^^^^^^^
│
└── Not at beginning
Therefore, ^Learning does not match.
^ Demo Program 1 - Successful Match
The target string is Learning Python is very easy and the Regular Expression is ^Learning.
The ^ symbol forces Learning to appear at the beginning.
The target starts with Learning, so re.search("^Learning", s) returns a Match Object.
Hence the condition res != None is True and the program prints the success message.
^ Demo Program 2 - Unsuccessful Match
The target is Python Learning is very easy.
The word Learning exists, but it does not occur at the beginning.
Python Learning is very easy
↑
Learning starts here
Because the pattern is ^Learning, Python requires Learning at the start.
Therefore, no match is found and re.search() returns None.
The $ Anchor - End-of-String Matching
The dollar symbol $ is used to check whether a pattern occurs at the end of the string.
Simple Definition:
$ represents the end of the string.
Syntax:
pattern$
Example:
easy$
This means the target string should end with easy.
Consider:
Learning Python is very easy
The word easy occurs at the end:
Learning Python is very easy
^^^^
│
└── End of string
Therefore, the match is successful.
But consider:
Learning Python is easy today
Although easy exists, it is not at the end.
Therefore, easy$ does not match.
$ Demo Program 1 - Successful Match
The target is Learning Python is very easy.
The pattern easy$ requires easy to occur at the end.
The target ends with exactly easy.
Therefore, re.search() returns a Match Object and the success message is displayed.
$ Demo Program 2 - Unsuccessful Match
The target contains easy, but the string actually ends with today.
The pattern easy$ does not simply ask whether easy exists.
It specifically asks whether easy occurs at the end.
Therefore, the match fails.
Using ^ and $ Together
^ and $ can be used together.
Example:
^Learning Python$
This means the complete target should start with Learning and end immediately after Python.
In other words, this pattern can be used to match the complete string Learning Python.
The pattern contains two anchors:
^ $
│ │
Start End
│ │
▼ ▼
Learning Python
The complete target is exactly Learning Python, so the pattern matches successfully.
^ and $ Together - Failure Program
The pattern ^Learning Python$ requires the string to end immediately after Python.
But the target contains additional characters:
Learning Python is easy
^^^^^^^^
Extra text
Therefore, the pattern does not match.
Case-Sensitive Matching by Default
By default, Python Regular Expression matching is case-sensitive.
This means:
Python
python
PYTHON
PyThOn
are treated as different combinations of uppercase and lowercase characters.
For example, Pattern = "python" and Target = "Python" do not match by default because:
p != P
In the program, the target contains Python but the pattern is python.
Target : Python
Pattern : python
↑
P != p
Regular Expression matching is case-sensitive by default, so no match is found.
re.IGNORECASE - Case-Insensitive Matching
Python provides the re.IGNORECASE flag when uppercase and lowercase differences should be ignored during matching.
Simple Definition:
re.IGNORECASE performs case-insensitive Regular Expression matching.
Syntax:
re.search(pattern, string, re.IGNORECASE)
With this flag, patterns such as python, Python, PYTHON and PyThOn can match without requiring the same capitalization.
In the program, the target contains Python and the pattern is python. Normally Python != python, but because we supplied re.IGNORECASE, Python ignores case differences while performing the match.
Pattern : python
Target : Python
│
▼
Ignore Case
│
▼
MATCH
Hence Match Found is printed.
re.I - Short Form of re.IGNORECASE
Python also provides re.I as a short form of re.IGNORECASE.
Therefore, both of the following can be used:
re.search("python", s, re.IGNORECASE)
and:
re.search("python", s, re.I)
In the program, the target contains PYTHON while the pattern is python. Because re.I is enabled, the case difference is ignored and they match successfully.
Case-Insensitive Matching with ^
The pattern is ^learning and the target starts with Learning.
There are two requirements:
^
│
└── Must occur at beginning
re.IGNORECASE
│
└── Ignore uppercase/lowercase difference
Therefore:
learning
Learning
are considered a successful match at the beginning.
Case-Insensitive Matching with $
The target ends with EASY and the pattern is easy$.
The $ requires easy to occur at the end.
The re.IGNORECASE flag ignores the difference between:
easy
EASY
Therefore, the match succeeds.
Using ^, $ and re.IGNORECASE Together
The target is LEARNING PYTHON and the pattern is ^learning python$.
Python checks three things:
1. ^
Pattern must start matching
at the beginning.
2. learning python
Required text must match.
3. $
Pattern must finish matching
at the end.
At the same time, re.IGNORECASE tells Python to ignore uppercase and lowercase differences.
Therefore:
LEARNING PYTHON
learning python
match successfully.
Complete Execution Flow
Start
│
▼
import re
│
▼
Define Pattern
│
▼
Define Target
│
▼
Is ^ Used in Pattern?
/ Yes No
│ │
▼ │
Match Must │
Start at │
Beginning │
│ │
└────┬─────┘
▼
Is $ Used in Pattern?
/ Yes No
│ │
▼ │
Match Must │
Finish at │
End │
│ │
└────┬─────┘
▼
Is re.IGNORECASE Used?
/ Yes No
│ │
▼ ▼
Ignore Case Match Case
Differences Exactly
│ │
└────┬─────┘
▼
Perform Match
│
▼
Match Found?
/ Yes No
│ │
▼ ▼
Match Object None
│ │
└────┬─────┘
▼
End
Anchors vs Flags - Key Differences
^ and $ are Regular Expression anchors.
They specify a position rather than matching ordinary text characters.
^
│
└── Beginning position
$
│
└── End position
re.IGNORECASE, on the other hand, is a flag.
It changes how matching is performed:
re.IGNORECASE
│
▼
Ignore Case
Differences
The following table summarizes the four features:
| Feature | Purpose | Example |
|---|---|---|
^ |
Match at beginning | ^Learning |
$ |
Match at end | easy$ |
re.IGNORECASE |
Ignore uppercase/lowercase differences | re.search("python", s, re.IGNORECASE) |
re.I |
Short form of re.IGNORECASE |
re.search("python", s, re.I) |
The type of each item:
| Item | Type |
|---|---|
^ |
Anchor |
$ |
Anchor |
re.IGNORECASE |
Flag |
Quick Revision and Summary
Quick Revision
^
│
└── Starts with
$
│
└── Ends with
re.IGNORECASE
│
└── Ignore uppercase /
lowercase differences
re.I
│
└── Short form of
re.IGNORECASE
Examples:
^Python
↓
Starts with Python
Python$
↓
Ends with Python
^Python$
↓
Complete string is Python
re.IGNORECASE
↓
Python = python = PYTHON
for case-insensitive matching
Summary
- The
^symbol represents the beginning of a string. - A pattern such as
^LearningrequiresLearningto occur at the beginning. - If
Learningoccurs somewhere else,^Learningdoes not match. - The
$symbol represents the end of a string. - A pattern such as
easy$requireseasyto occur at the end. ^and$can be combined to constrain both ends of a match.- For example,
^Learning Python$matches the complete stringLearning Python. - Python Regular Expression matching is case-sensitive by default.
Pythonandpythonare different during normal case-sensitive matching.re.IGNORECASEenables case-insensitive matching.re.Iis the short form ofre.IGNORECASE.^and$are anchors, whilere.IGNORECASEis a flag.- Anchors and flags can be combined in the same Regular Expression operation.
- The ^ anchor requires a pattern to occur at the beginning of the string
- The $ anchor requires a pattern to occur at the end of the string
- Using ^ and $ together constrains the match to the complete string
- re.IGNORECASE (and its short form re.I) ignores uppercase and lowercase differences during matching
- ^ and $ are anchors that describe positions, while re.IGNORECASE is a flag that changes matching behaviour