Open links in new tab
  1. Regular expressions (RegEx) are sequences of characters that form search patterns. They are used to check if a string contains a specified search pattern, and Python provides a built-in package called re to work with regular expressions.

    Basic Usage

    To use regular expressions in Python, you need to import the re module. Here is an example of how to search a 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)
    print(x)
    Copied!

    Common Functions

    The re module offers several functions to work with regular expressions:

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

    Example of findall Function

    import re

    txt = "The rain in Spain"
    x = re.findall("ai", txt)
    print(x)
    Copied!

    Example of search Function

    import re

    txt = "The rain in Spain"
    x = re.search("\s", txt)
    print("The first white-space character is located in position:", x.start())
    Copied!
  1. Python RegEx - W3Schools

    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.

  2. Python RegEx - GeeksforGeeks

    Aug 14, 2025 · This Python code uses regular expressions to search for the word "portal" in the given string and then prints the start and end indices of the matched word within the string.

  3. Python RegEx (With Examples) - Programiz

    In this tutorial, you will learn about regular expressions (RegEx), and use Python's re module to work with RegEx (with the help of examples).

  4. Python - Regular Expressions - Online Tutorials Library

    A regular expression in python is often abbreviated as "regex" or "regexp". This is a sequence of characters used to search for specific patterns within text. It provides a specialized syntax that allows …

  5. Regular Expressions in Python: Basic Syntax

    May 3, 2024 · Regular expressions use a sequence of characters to define a search pattern. Here's a quick overview of some common regex syntax elements in Python: (Dot): Matches any single …

  6. People also ask
  7. Python Regular Expressions: A Comprehensive Guide with Examples

    Apr 5, 2025 · Regular expressions (regex) are a powerful tool in Python for pattern matching and text manipulation. They allow you to define search patterns that can be used to find, extract, or replace …

  8. Python Regular Expressions - Google Developers

    Jul 23, 2024 · Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and...

  9. Regular Expression in Python - PYnative

    Jul 27, 2021 · We will start this tutorial by using the RE module, a built-in Python module that provides all the required functionality needed for handling patterns and regular expressions. Type import re at the …

  10. Regular Expressions in Python - TutorialsTeacher.com

    Different functions in Python's re module use raw string as an argument. A normal string, when prefixed with 'r' or 'R' becomes a raw string. rawstr = r'Hello! How are you?' print(rawstr) #Hello! How are you?