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--
Editor to share with you the example analysis of RE regular expressions in python3, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Introduce regular module (Regular Expression)
To use RE in python3, re module must be introduced.
Import re # introduces regular expressions
two。 The main method used is match (), which matches from left to right
# pattern is the rule to be verified # str is the string result = re.match (pattern, str) to be verified # if result is not None, the group method extracts data from result
3. Regular expression
1 ️single character matching rule
Character function. Match any one character (except\ n) [] match the characters enumerated in [] match numbers, that is, 0-9\ D matches non-numeric characters, that is, match non-numeric characters\ s match blank characters, that is, space\ tab\ S matches non-white space characters,\ s invert\ w accompany word characters, amusic z, AME Z, 0-9, _\ W match non-word characters \ W reverse
2 ️rules for quantity
Character function * match the previous character 0 times or unlimited times, optional, more or less + match the previous character 1 times or indefinitely until once? Matching the previous character once or 0 times, either once or without {m} matching the previous character m times {m} matching the previous character at least m times {m ~ n} matching the previous character m to n times
Example 1: verify whether the mobile phone number conforms to the rules (regardless of boundary issues)
# first of all, know the rule of mobile phone number # 1. It's all the number two. The length is 11.3. The first place is 14. The second bit is a bit in 35678 pattern = "1 [35678]\ d {9}" phoneStr = "18230092223" result = re.match (pattern, phoneStr) result.group () # the execution result is as follows:
4. For the original string raw, let's take a look at the following example:
In the image above: after adding "r" before assigning "\ nabc" to str, the python interpreter automatically adds a "\" to the value "\ nabc" of str.
Enables str to keep the value of the original string "\ nabc" printed when it is printed.
Example 2: (application of original string in regular expression)
If there is no original self-pay r, we have to do the following: add double "\" to pattern to avoid reducing "\" in escape characters. It will be troublesome.
When we use the r original string, we do not have to consider the string transfer problem, and it is easier to solve the character matching problem.
5. Represent the boundary
Character function ^ match string beginning $match string end\ b match the boundary of a word\ B match non-word boundary
Example 3: boundary (making rules to match str= "ho ve r")
Import re # defines rules to match str= "ho ve r" # 1. Start with the letter # 2. There is an empty character # 3 in the middle. Ve defines the matching word boundary pattern = r "^\ w +\ s\ bve\ b\ sr" str = "ho ve r" result = re.match (pattern, str) result.group ()
6. Matching grouping
Character function | match any expression left or right (ab) use the characters in parentheses as a grouping\ num references the string matched by the grouping num (? P) group aliases (? P=name) refer to the string matched by the name group
Example 4: match the number between 0 and 100
Import re # matches the numbers between 0 and 100. first of all, regular matches start from left to 100. after analysis, 0-100 can be divided into three parts: # 1. 0 "0 $" # 2. 100 "100 $" # 3. 1-99 "[1-9]\ d {0Magin1} $" # so integrate the following pattern = r "0 $| 100$ | [1-9]\ d {0parentin 1} $" # Test data is 0Jing 3Mo 27100123 result = re.match (pattern) "27") result.group () # takes 0 into account on 1-99. The above pattern can also be abbreviated as: pattern=r "100 $| [1-9]?\ d {0jol 1} $" # Test results are shown below:
Example 5: match the grouping to get the content in the tags in the page
Import re# matches the grouping to get the content in the page tags. Crawlers will use str = "hello world!" pattern = r "(. *)" result = re.match (pattern, str) result.group () # execute as shown below
Example 6: group references to accurately obtain the contents of multiple tags
Import re # reference grouping to accurately get the content within multiple tags # "\ 1" is a reference to the first grouping, similarly. Str = "hello world!" pattern = r ". *" result = re.match (pattern, str) result.groups () # as shown below:
Example 7-2: alias for grouping
Import re # is grouped with the alias str = "hello world!" pattern = "(? P.*)" result = re.match (pattern, str) result.groups () # as shown below:
The above is all the content of the article "sample Analysis of RE regular expressions in python3". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.