📒
PyCoders Handbook
  • Welcome!
  • Values and Principles
  • Onboarding Guideline
  • Python
    • Week 1
      • 💻Introduction to Python
    • Week 2
      • 🗃️Lists, Tuples, Sets, Dictionaries
    • Week 3
      • 🤖Functions
      • 📦Modules & Packages
    • Week 4
      • 🛠️Errors & Exceptions
      • 💾File I/O (Input/Output)
    • Week 5
      • ⚙️OOP-1
    • Week 6
      • ⚙️OOP-2
    • Module Project
  • Database
    • Week 8
      • 🗄️Introduction to Databases & SQL
    • Week 9
      • 🐘PostgreSQL
    • Week 10
      • 🍃MongoDB
    • Module Project
  • DATA GATHERING
    • Week 13
      • 🌐Web Technologies & APIs
    • Week 14
      • ⚡Python Requests Library & FastAPI
  • Maths/Stats
    • 🔢Introduction to Statistics
    • Assignment
  • DATA MANIPULATION
    • Week 17
    • Week 18
    • Week 19
  • Data Visualization-1
    • Week 20
    • Week 20.5
      • Python graph gallery
    • Week 21
  • DATA MANIPULATION PROJECT
    • Week23-24-25 (Project Weeks)
  • DATA VISUALIZATON-2
    • Week26(PowerBI)
  • Machine Learning
    • Week27
    • Week 28
    • Capstone Project
  • Extra Documentation
    • Week27-2
    • Copy of Week 16
    • RegEx
    • PyQt5
    • ML
    • ML
    • ML
    • ML
    • ML
    • maths-stats
    • maths-stats
    • maths-stats
    • Module Project(Weather Application)
    • Tableau
    • Module Project(AutoScout24 Application)
    • ⚙️Web Scraping
    • Week28-2
Powered by GitBook
On this page
  • Python RegEx
  • RegEx Module
  • RegEx in Python

Was this helpful?

  1. Extra Documentation

RegEx

Python RegEx

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.

RegEx can be used to check if a string contains the specified search pattern.

RegEx Module

Python has a built-in package called re, which can be used to work with Regular Expressions.

Import the re module:

import re

RegEx in Python

When you have imported the re module, you can start using regular expressions:

Example

import re

# Check if the string starts with "The" and ends with "Spain":

txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)

if x:
  print("YES! We have a match!")
else:
  print("No match")
```

Output
```
YES! We have a match!

RegEx Functions

The re module offers a set of functions that allows us to search a string for a match:

Function

Description

findall

Returns a list containing all matches

search

Returns a Match object if there is a match anywhere in the string

split

Returns a list where the string has been split at each match

sub

Replaces one or many matches with a string

Metacharacters

Metacharacters are characters with a special meaning:

Example 1: re.findall()

# Program to extract numbers from a string

import re

string = 'hello 12 hi 89. Howdy 34'
pattern = '\d+'

result = re.findall(pattern, string) 
print(result)

# Output: ['12', '89', '34']

If the pattern is not found, re.findall() returns an empty list.

Example 2: re.split()

import re

string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+'

result = re.split(pattern, string) 
print(result)

# Output: ['Twelve:', ' Eighty nine:', '.']

If the pattern is not found, re.split() returns a list containing the original string.

Example 3: re.sub()

# Program to remove all whitespaces
import re

# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters
pattern = '\s+'

# empty string
replace = ''

new_string = re.sub(pattern, replace, string) 
print(new_string)

# Output: abc12de23f456

If the pattern is not found, re.sub() returns the original string.

PreviousCopy of Week 16NextPyQt5

Last updated 2 years ago

Was this helpful?

More details can be found in and

Regular expression operations
Regular Expression HOWTO