Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Regular Expressions

Regular Expressions:

 

Basics of Regular Expressions:

1. What are Regular Expressions?

  • Regular expressions are patterns used to match strings.
  • They provide a flexible way to search, extract, and manipulate text.

2. Importing the re Module:

  • Import the built-in re module to work with regular expressions.

Using Regular Expressions:

1. Matching:

  • Use the re.match() function to check if a string starts with a specified pattern.
  • The pattern is defined using regular expression syntax.

2. Searching:

  • Use the re.search() function to find the first occurrence of a pattern in a string.

3. Finding All Matches:

  • Use the re.findall() function to find all occurrences of a pattern in a string.
  • Returns a list of matching substrings.

4. Finding and Replacing:

  • Use the re.sub() function to replace occurrences of a pattern with a specified string.

Regular Expression Syntax:

1. Literal Characters:

  • Matching literal characters exactly.

2. Character Classes:

  • Using character classes like [a-zA-Z] to match a range of characters.
  • Special character classes like \d (digits), \w (word characters), \s (whitespace), etc.

3. Quantifiers:

  • Using quantifiers like *, +, ?, {n}, {n,m} to specify repetition.

4. Anchors:

  • Using anchors like ^ (start of line) and $ (end of line) to specify position.

5. Groups and Alternation:

  • Using parentheses to group parts of the pattern.
  • Using | (pipe) to indicate alternation (OR) between patterns.

Flags:

1. Flags for Modifying Behavior:

  • Using flags like re.IGNORECASE for case-insensitive matching.
  • Modifying how patterns are matched using flags.

Example:

Here's a simple example illustrating the use of regular expressions:

import re

# Matching a pattern
pattern = r"\d{3}-\d{2}-\d{4}"
text = "My SSN is 123-45-6789"
match = re.search(pattern, text)
if match:
print("Social Security Number found:", match.group())

# Finding all matches
emails = "Contact us at alice@example.com and bob@example.com"
email_pattern = r"\w+@\w+\.\w+"
all_matches = re.findall(email_pattern, emails)
print("Email addresses:", all_matches)
     

 

 

In this example, we import the re module, define a regular expression pattern to match Social Security Numbers and email addresses, and then use re.search() and re.findall() to find matches.

Regular expressions are incredibly versatile, but they can also be complex. Understanding the syntax and practicing with various patterns will help you become proficient in using regular expressions effectively.