Nearby lessons

123 of 159

Python - Regex Anchors and Flags

📌 What You Will Learn
  • 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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very easy"
4 
5res = re.search("^Learning", s)
6 
7if res != None:
8 print("Target String starts with Learning")
9else:
10 print("Target String not starts with Learning")
Output
Target String starts with Learning

^ 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.

🐍Code Cell
1import re
2 
3s = "Python Learning is very easy"
4 
5res = re.search("^Learning", s)
6 
7if res != None:
8 print("Target String starts with Learning")
9else:
10 print("Target String not starts with Learning")
Output
Target String not starts with Learning

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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very easy"
4 
5res = re.search("easy$", s)
6 
7if res != None:
8 print("Target String ends with easy")
9else:
10 print("Target String not ends with easy")
Output
Target String ends with easy

$ 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.

🐍Code Cell
1import re
2 
3s = "Learning Python is easy today"
4 
5res = re.search("easy$", s)
6 
7if res != None:
8 print("Target String ends with easy")
9else:
10 print("Target String not ends with easy")
Output
Target String not ends with easy

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.

🐍Code Cell
1import re
2 
3s = "Learning Python"
4 
5res = re.search("^Learning Python$", s)
6 
7if res != None:
8 print("Complete String Matched")
9else:
10 print("Complete String Not Matched")
Output
Complete String Matched

^ 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.

🐍Code Cell
1import re
2 
3s = "Learning Python is easy"
4 
5res = re.search("^Learning Python$", s)
6 
7if res != None:
8 print("Complete String Matched")
9else:
10 print("Complete String Not Matched")
Output
Complete String Not Matched

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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very easy"
4 
5res = re.search("python", s)
6 
7if res != None:
8 print("Match Found")
9else:
10 print("Match Not Found")
Output
Match Not 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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very easy"
4 
5res = re.search("python", s, re.IGNORECASE)
6 
7if res != None:
8 print("Match Found")
9else:
10 print("Match Not Found")
Output
Match Found

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.

🐍Code Cell
1import re
2 
3s = "Learning PYTHON is very easy"
4 
5res = re.search("python", s, re.I)
6 
7if res != None:
8 print("Match Found")
9else:
10 print("Match Not Found")
Output
Match Found

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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very easy"
4 
5res = re.search("^learning", s, re.IGNORECASE)
6 
7if res != None:
8 print("Target starts with Learning")
9else:
10 print("Target does not start with Learning")
Output
Target starts with Learning

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.

🐍Code Cell
1import re
2 
3s = "Learning Python is very EASY"
4 
5res = re.search("easy$", s, re.IGNORECASE)
6 
7if res != None:
8 print("Target ends with easy")
9else:
10 print("Target does not end with easy")
Output
Target ends with easy

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.

🐍Code Cell
1import re
2 
3s = "LEARNING PYTHON"
4 
5res = re.search(
6 "^learning python$",
7 s,
8 re.IGNORECASE
9)
10 
11if res != None:
12 print("Complete String Matched")
13else:
14 print("Complete String Not Matched")
Output
Complete String Matched

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 ^Learning requires Learning to occur at the beginning.
  • If Learning occurs somewhere else, ^Learning does not match.
  • The $ symbol represents the end of a string.
  • A pattern such as easy$ requires easy to occur at the end.
  • ^ and $ can be combined to constrain both ends of a match.
  • For example, ^Learning Python$ matches the complete string Learning Python.
  • Python Regular Expression matching is case-sensitive by default.
  • Python and python are different during normal case-sensitive matching.
  • re.IGNORECASE enables case-insensitive matching.
  • re.I is the short form of re.IGNORECASE.
  • ^ and $ are anchors, while re.IGNORECASE is a flag.
  • Anchors and flags can be combined in the same Regular Expression operation.
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10