In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use regular expressions in python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn "how to use regular expressions in python".
I. Preface
When we do interface automation, when dealing with the relevant data that the interface depends on, we usually use regular expressions to extract the relevant data.
Regular expressions are used in python, which can be implemented using the official library re.
Second, the use of re module
To use regular expressions in python, you need to use the re module to operate. Here we introduce several methods commonly used in the re module.
01 re.match function
Parameter description: receive two parameters
The first ① is the matching rule.
The second ② is the matching target string
Re.match attempts to match a pattern from the beginning of the string.
If the match is successful, a matching object (which contains our matching information) is returned.
Match () returns null if the starting position match is not successful.
Case study:
02 re.search method
Parameter description: receive two parameters
The first ① is the matching rule.
The second ② is the matching target string
Re.search scans the entire string and returns the first successful match.
Case study:
The difference between re.match and re.search:
① re.match matches from the beginning of the string. If the beginning of the string does not conform to the regular expression, the match fails and the function returns empty.
② and re.search match the entire string and return it until a successful match is found. If no successful match is found in the whole string, empty is returned.
03 findall method
Parameter description: receive two parameters
The first ① is the matching rule.
The second ② is the matching target string
Finds all the substrings matched by the regular expression in the string and returns a list, or an empty list if no matches are found.
Case study:
Note: match and search match one result, and findall matches all the results that conform to the rules.
04 sub method
To replace some characters in a string, you can use regular expressions to match the selected substring.
Parameters:
Pattern: matching rules
Repl: new content to be replaced after matching
String: a string that needs to be replaced by rules
Count: the number of replacements. Parameters may not be passed, and all those that conform to the rules can be replaced by default.
Case study:
05 greed mode explains
Greedy mode: the default quantifier in Python is greedy, always trying to match as many characters as possible
The following are examples:
There is a string s, we need to match more than 3 numbers in the string, there are 8 numbers in the string, greedy pattern will match as many characters as possible, more than 3, 8 is also more than 3, so the matching result here is 8 numbers.
Non-greedy mode: always try to match as few characters as possible. Add? after "*", "?", "+", "{mforce n}", {m,} to turn off greedy mode.
After turning off greed mode, get as little as possible.
As follows, only the first three values are obtained (at least 3 in the case of rules, non-greedy is to match the first three arrays that conform to the rules)
About the re module, more usage, this article does not do too much introduction, everyone to study, thank you!
The basic matching of regular expressions is attached for your reference.
Third, regular expression grammar
01 represents a single character
Single character: represents a single character, such as\ d for matching numbers and\ D for matching non-digits.
The specific rules are as follows:
02 indicates quantity
If you want to match a character multiple times, you can add a quantity after the character to represent it.
The specific rules are as follows:
03 represents the boundary
Used to indicate the boundaries of a string or word, such as the beginning of a string, the beginning of a word, etc.
04 matching grouping
Group the matching content.
At this point, I believe you have a deeper understanding of "how to use regular expressions in python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.