In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.