Basic Patterns and Literals
2 min read ·
Regular expressions start with simple patterns called literals. These are exact characters used to match text.
Simple Matching
A basic pattern matches exact text.
If pattern exists in the string, it returns a match.
If pattern does not exist:
Note
Basic patterns match exact characters as they appear in the string.
Case Sensitivity
Regular expressions are case sensitive by default.
Output will be None because case does not match.
Case Insensitive Matching
You can ignore case using
re.IGNORECASE.Pro Tip
Use IGNORECASE when user input can have different letter cases.
Raw Strings
In Python, backslash has special meaning. To avoid issues, raw strings are used in regex.
Without Raw String
With Raw String
Caution
Always prefer raw strings for regex patterns to avoid unexpected behavior.
Combining Concepts
Exercise
- Search for a word in a sentence
- Try case sensitive and insensitive matching
- Use raw string to find digits in text