Category: 2. Python Regex
-
Python Regex Lookahead
Summary: in this tutorial, you’ll learn about Python regex lookahead and negative lookahead. Introduction to the Python regex lookahead Sometimes, you want to match X but only if it is followed by Y. In this case, you can use the lookahead in regular expressions. The syntax of the lookahead is as follows: This syntax means to search for X but matches…
-
Python Regex Non-capturing Group
Summary: in this tutorial, you’ll learn about the Python regex non-capturing group to create a group but don’t want to store it in the groups of the match. Introduction to the Python regex non-capturing group Regular expressions have two types of groups: So far, you learned how to use a capturing group to extract information from a…
-
Python Regex Alternation
Summary: in this tutorial, you’ll learn about Python regex alternation, which behaves like the “OR” operator in regular expressions. Introduction to the Python regex alternation To represent an alternation in regular expressions, you use the pipe operator (|). The pipe operator is called the alternation. It is like the or operator in Python. The following regular expression uses an…
-
Python Regex Backreferences
Summary: in this tutorial, you’ll learn about Python regex backreferences and how to apply them effectively. Introduction to the Python regex backreferences Backreferences like variables in Python. The backreferences allow you to reference capturing groups within a regular expression. The following shows the syntax of a backreference: Alternatively, you can use the following syntax: In this syntax, N can be 1, 2,…
-
Python Regex Capturing Group
Summary: in this tutorial, you’ll learn about Python regex capturing groups to create subgroups for a match. Introduction to the Python regex capturing groups Suppose you have the following path that shows the news with the id 100 on a website: The following regular expression matches the above path: Note that the above regular expression also matches…
-
Python Regex Sets & Ranges
Summary: in this tutorial, you’ll learn how to use the sets and ranges to create patterns that match a set of characters. Several characters or character sets inside square brackets [] mean matching for any character or character set among them. Sets For example, [abc] means any of three characters. ‘a’, ‘b’, or ‘c’. The [abc] is called a set. And you can use…
-
Python Regex Non-Greedy
Summary: in this tutorial, you’ll learn about the regex non-greedy (or lazy) quantifiers that match their preceding elements as few times as possible. Introduction to the regex non-greedy (or lazy) quantifiers Quantifiers allow you to match their preceding elements a number of times. Quantifiers work in one of two modes: greedy and non-greedy (lazy). When quantifiers work in…
-
Python Regex Greedy
Summary: in this tutorial, you’ll learn about the Python regex greedy mode and how to change the mode from greedy to non-greedy. By default, all quantifiers work in a greedy mode. It means that the quantifiers will try to match their preceding elements as much as possible. Let’s start with an example to understand how the regex…
-
Python Regex Quantifiers
Summary: in this tutorial, you’ll learn how to use Python regex quantifiers to define how many times a character or a character set can be repeated. Introduction to Python regex quantifiers In regular expressions, quantifiers match the preceding characters or character sets a number of times. The following table shows all the quantifiers and their meanings:…
-
Python Regex Word Boundary
Summary: in this tutorial, you’ll learn how to construct regular expressions that match word boundary positions in a string. Introduction to the Python regex word boundary A string has the following positions that qualify as word boundaries: The following picture shows the word boundary positions in the string “PYTHON 3!”: In this example, the “PYTHON 3!” string has…