In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Python regular rule example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Python regular rule example analysis" bar!
Problem recurrence
As we all know, Python has a regular rule that almost all online blog posts tell you that this rule matches alphanumerics and underscores, but this is not the case:
The Python2 code is as follows:
~ | ⇒ pythonPython 2.7.10 (default, Aug 17 2018, 19:45:58) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwinType "help", "copyright", "credits" or "license" for more information. > > import re > aa = 'Snake catcher' > re.match ('\ w {1pm 20}', aa) > > bb = 'abc123ADB' > > re.match ('\ w {1pm 20}', bb)
We can see that\ w cannot match Chinese in python2. So what does the same code look like when it runs in Python3?
~ | ⇒ python3Python 3.7.1 (default, Nov 28 2018, 11:55:14) [Clang 9.0.0 (clang-900.0.39.2)] on darwinType "help", "copyright", "credits" or "license" for more information. > > import re > aa = 'Snake catcher' > > re.match ('\ w {1d20}', aa) > > bb = 'abc123ADB' > > re.match ('\ w {1pm 20}', bb)
But in Python3,\ w can match Chinese. What's going on? To answer this question, we need to go back to the official Python documentation to find the answer.
Solve the problem
When we carefully read the official documentation of Python, we will find that there is a big difference between Python 2 and Python3 for the same regular rule, so let's take a look at Python2:
When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9]. With LOCALE, it will match the set [0-9] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9] plus whatever is classified as alphanumeric in the Unicode character properties database.
When the LOCALE (re.L) and UNICODE (re.U) flags are not set, it matches numeric letters and underscores, and if LOCALE (re.L) is set, it matches numeric underscores and LOCALE text. If the UNICODE (re.U) flag is set, it matches numeric underscores and characters in the Unicode character set.
So Python3:
For Unicode (str) style: characters that match Unicode words contain most of the characters that can make up words, including numbers and underscores. If the ASCII flag is set, it only matches [a-zA-Z0-9 _]. For the 8-bit (bytes) style: matches numbers and letters and underscores in ASCII characters, which is [a-zA-Z0-9 _]. If the LOCALE flag is set, it matches the numbers and letters and underscores of the current language region.
At this point, I see that by default, no flags are set, Python2\ w matches characters in the ASCII character set, including numeric characters and underscores, and Python3\ w matches numeric underscores and Unicode character sets. So, for ease of migration, if you want to match characters in the ASCII character set, specify the flag re.A, and if you want to match the characters in the Unicode character set, specify the flag re.U.
Thank you for your reading, the above is the content of "Python regular rule example analysis", after the study of this article, I believe you have a deeper understanding of the Python regular rule example analysis of this problem, 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.