Category: 2. Python Regex

  • Python Regex Cheat Sheet

    This page provides a Python regex cheat sheet that you can quickly reference while working with regular expressions. Character sets Pattern Meaning \w Match a single word character a-z, A-Z, 0-9, and underscore (_) \d Match a single digit 0-9 \s Match whitespace including \t, \n, and \r and space character . Match any character except…

  • Python Regex Flags

    Summary: in this tutorial, you’ll learn about the Python regex flags and how they change the behavior of the regex engine for pattern matching. Introduction to the Python regex flags The regular expression functions like findall, finditer, search, match, split, sub, … have the parameter (flags) that accepts one or more regex flags. Since Python 3.6, regex flags are instances of…

  • Python Regex split()

    Summary: in this tutorial, you’ll learn how to use the Python regex split() function to split a string at the occurrences of matches of a regular expression. Introduction to the Python regex split() function The built-in re module provides you with the split() function that splits a string by the matches of a regular expression. The split() function has the following syntax: In this…

  • Python Regex sub()

    Summary: in this tutorial, you’ll learn about the Python regex sub() function that returns a string after replacing the matched pattern in a string with a replacement. Introduction to the Python regex sub function The sub() is a function in the built-in re module that handles regular expressions. The sub() function has the following syntax: In this syntax: The sub() function searches for the pattern in…

  • Python Regex search()

    Summary: in this tutorial, you’ll learn how to use the Python regex search() function to return the first match of a pattern in a string. Introduction to the Python regex search() function The regex search() is a function in the built-in re module that deals with regular expressions. The search() function has the following syntax: In this syntax: The search() function scans the string from left to right…

  • Python Regex match()

    Summary: in this tutorial, you’ll learn how to use the Python regex match() function to find a match with a pattern at the beginning of a string. Introduction to the Python regex match function The re module has the match() function that allows you to search for a pattern at the beginning of the string: In this syntax: If the search is successful, the match() function…

  • Python Regex finditer()

    Summary: in this tutorial, you’ll learn how to use the Python regex finditer() function to find all matches in a string and return an iterator that yields match objects. Introduction to the Python regex finditer function The finditer() function matches a pattern in a string and returns an iterator that yields the Match objects of all non-overlapping matches. The following shows the syntax…

  • Python Regex fullmatch()

    Summary: in this tutorial, you’ll learn how to use the Python regex fullmatch() to match the whole string with a regular expression. Introduction to the Python regex fullmatch function The fullmatch() function returns a Match object if the whole string matches the search pattern of a regular expression, or None otherwise. The syntax of the fullmatch() function is as follows: In this syntax: Python regex fullmatch…

  • Python Regex findall()

    Summary: in this tutorial, you’ll learn how to use the Python regex findall() function to find all matches of a pattern in a string. Introduction to the Python regex findall() function The findall() is a built-in function in the re module that handles regular expressions. The findall() function has the following syntax: In this syntax: The findall() function scans the string from left to right and finds all the matches…

  • Python Regex Lookbehind

    Summary: in this tutorial, you’ll learn about Python regex lookbehind and negative lookbehind. Introduction to the Python regex lookbehind In regular expressions, the lookbehind matches an element if there is another specific element before it. The lookbehind has the following syntax:(?<=Y)X In this syntax, the pattern will match X if there is Y before it. For example, suppose you have…