Python Course Guide
  • Introduction to Python
  • Guides
    • Course Outline
    • Getting started with Python
    • Syntax
    • Variables
    • Datatypes
    • Casting
    • Numbers
    • Operators
      • Python Math
    • Strings
    • Python String Formatting
    • User Input
    • Git and Github
    • Booleans
    • List
    • Tuples
    • Sets
    • Dictionaries
    • Conditionals
    • Loops
    • Function
    • Arrays
    • Try and Except
    • Scope
    • Classes and Objects
    • Class Methods and Properties
    • Inheritance
    • Polymorphism
    • Dunder Methods(Double Underscore Methods)
    • Decorators
    • Iterator and Generator
    • Modules and Packages
    • Date and Time
    • JSON
    • RegEx
    • Pip
    • File Handling
    • Data Structures and Alogrithms
    • References
Powered by GitBook
On this page
  • RegEx Module
  • RegEx in Python
  1. Guides

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

Search the string to see if it starts with "The" and ends with "Spain":

import re

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

Last updated 2 years ago