Get the App
SLTechnology News&Howtos  ›  Development  › 

Matching rules of regular expressions in Python

Shulou Source: shulou.com Published: 2022-06-03 15:02:28 09月29日 Update

This article mainly explains "the matching rules of regular expressions in Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the matching rules of regular expressions in Python".

1. Understanding regular expressions

A regular expression is a logical formula for manipulating a string, that is, a "regular string" is formed by predefined specific characters and a combination of these specific characters. This "regular string" is used to express a filtering logic for a string.

Regular expressions are a very powerful tool for matching strings. There is also the concept of regular expressions in other programming languages, and Python is no exception. Using regular expressions, it is easy for us to extract what we want from the content of the returned page.

The approximate matching process of a regular expression is:

1. Compare the characters in the expression and the text in turn

two。 If every character can be matched, the match is successful; once there are unsuccessful characters, the match fails.

3. If there are quantifiers or boundaries in the expression, the process is slightly different.

two。 Grammatical rules of regular expressions

Here are some matching rules for regular expressions in Python

20130515113723855

3. Comments on regular expressions

(1) greedy mode and non-greedy mode of quantifiers.

Regular expressions are often used to find matching strings in text. The default quantifier in Python is greedy (or, in a few languages, it may be non-greedy), always trying to match as many characters as possible; non-greedy, on the contrary, always trying to match as few characters as possible. For example: the regular expression "ab" will find "abbb" if it is used to find "abbbc". If you use the non-greedy quantifier "ab?", you will find "a".

Note: we generally use non-greedy mode to extract.

(2) backslash problem

As with most programming languages, regular expressions use "\" as an escape character, which can cause backslash trouble. If you need to match the character "\" in the text, you will need four backslashes in a regular expression expressed in a programming language: the first two and the last two are used to escape to a backslash in the programming language. converted to two backslashes and then escaped into a backslash in the regular expression.

The native strings in Python solve this problem well, and the regular expressions in this example can be represented by r "\". Similarly, the "\ d" that matches a number can be written as r "\ d".

Thank you for your reading, the above is the content of "the matching rules of regular expressions in Python". After the study of this article, I believe you have a deeper understanding of the matching rules of regular expressions in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Expressions regular characters rules greed strings slashes languages content programming languages programming two text patterns escape problems learning success that is as much as possible Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL Shulou Technology Microsoft vpn