In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use re.findall() and re.compile() methods in Python 3". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to use re.findall() and re.compile() methods in Python 3" together!
re.findall() method and re.compile()
re.findall() finds all substrings in the string that match the regular expression and returns a list; if no match is found, returns an empty list.
The return result is a list type, and you need to traverse it to get each group of contents in turn.
findall(patern, string, flags=0)
pattern : A pattern string in a regular.
string : The original string to be searched for and replaced.
flags : flags, used to control the matching method of regular expressions, such as: case-sensitive, multi-line matching, etc.
1. Example import re content = 'Hello 123456789 Word_This is just a test 666 Test'results = re.findall('\d+', content) print(results)for result in results: print(result)
Results:
['123456789', '666']
123456789
666
Process finished with exit code 0
Note that there are no groups () and groups() with the re.findall() function because the result is a list.
re. compile () method
The re.compile() method compiles regular strings into regular expression objects for reuse in later matches.
re.compile(pattern[, flags])
We can ignore matching patterns such as newlines in re.compile(), so we don't need to pass them in search() and findall() methods.
Thus, the re.findall() method can be expressed in two ways:
import re content = 'one1two22three33four4444'pattern = re.compile(r'\d+')print('=== method1:===')result1 = re.findall(pattern, content)print(result1) print('== method2 ===')result2 = pattern.findall(content)print(result2)
Results:
Method 1:===
['1', '22', '333', '4444']
Method 2===
['1', '22', '333', '4444']
Process finished with exit code 0
3."Pit" when using findall() method
Note the use of parentheses () in regular expressions
(1) When there is no parenthesis in the regular expression, it matches normally:
import re str1 = '2345 3456 4567 5678 6789'pattern_1 = re.compile ('\w+\s+\w+') # \w means match any word character including underscore, equivalent to [A-Za-z0-9_]print(pattern_1.findall(str1))
Results:
['2345 3456', '4567 5678']
Process finished with exit code 0
(2) When there is a parenthesis in a regular expression, its output is the content matched by the parenthesis, not the result matched by the whole expression:
import re str1 = '2345 3456 4567 5678 6789'pattern_1 = re.compile ('(\w+)\s+\w+') # \w means match any word character including underscore, equivalent to [A-Za-z0-9_]print(pattern_1.findall(str1))
Results:
['2345', '4567']
Process finished with exit code 0
The entire regular expression is executed, but only the content matched by the parentheses is output, that is, the content matched by the first (\w+) is output:
In the first match, as in the case without brackets, the match is "2345 3456", but only the result of (/w+) matching is output, that is,"2345";
The second match is similar, starting with "4567" and matching to "4567 5678", but still only outputs the result of (/w+) matching, i.e."4567".
(3) When there are two parentheses in the regular expression, its output is a list containing two tuples:
import re str1 = '2345 3456 4567 5678 6789'pattern_1 = re.compile ('((\w+)\s+\w+)') # \w means match any word character including underscore, equivalent to [A-Za-z0 -9_]print(pattern_1.findall(str1))
Results:
[('2345 3456', '2345'), ('4567 5678', '4567')]
Process finished with exit code 0
As you can see from the output, the result contains two tuples, each containing two strings.
The first tuple is the result of the first match, where the first string "2345 3456" is the outermost parenthesis of the regular expression.
((\w+)\s+\w+)
matching the output result;
The second string "2345" in the first tuple is the parenthesis in the regular expression
(\w+)
match the output result;
The second tuple is the result of the second match, and the matching principle is the same as the first match.
regular expression findall function problem
There are always problems when writing regular expressions, especially when the expression has multiple tuples. Let's see what happens when the findall() function in the re module meets multiple expression tuples.
The code is as follows:
import restr="a b c d"regex0=re.compile("((\w )\s \w )")print(regex0.findall(str))regex1=re.compile("(\w )\s \w ")print(regex1.findall(str))regex2=re.compile("\w \s \w ")print(regex2.findall(str))
Results:
[('a b', 'a'), ('c d', 'c')]
['a', 'c']
['a b', 'c d']
The result may be a bit unexpected, explain below
The first regular expression is with 2 parentheses, and we can see that its output is a list containing 2 tuples
The second regular expression has a parenthesis in it, and its output is what the parenthesis matches, not what the entire expression matches.
The third regular expression does not have parentheses, and its output is what the entire expression matches.
Thank you for reading, the above is "Python3 re.findall() and re.compile() method how to use" the content, after the study of this article, I believe that we have a deeper understanding of Python3 re.findall() and re.compile() method how to use this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.