In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand the conditional matching of Python3 regular expressions (? (id/name) yes-pattern | no-pattern)". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
1. Use
two。 Parameter meaning
3. Grammar
4. Detailed explanation of usage
4.1 remove the question mark after (\ d +), that is, there must be group 1, not optional
4.2 change the matching string
4.3 situations where group is optional (add question mark after group)
4.4 only match the group 1 part, but not the yes part
5. Conclusion
1. Through the comparison of 4.1 4.2 4.3, we can find that:
two。 Only the group part can be matched. If the yes-pattern and no-pattern do not match, only the group matching content will be output.
1. Use
((id/name) yes-pattern | no-pattern) is used to:
For the given id or name, try to match the contents of the yes-pattern section first
If the id or name conditions are not met, match the contents of the no-pattern section
This sentence still sounds like a mouthful, or it is very difficult to understand all of a sudden.
two。 Parameter meaning
The name or id here is for something that has been grouped through group before a conditional match.
Name: if it is a named group, that is, named group, then the corresponding packet has a corresponding name, that is, the corresponding name.
Id: if it is an unnamed packet, that is, unnamed group, then the corresponding packet also has the corresponding packet number, number called group, also known as id, which corresponds to the id here.
Yes-pattern: if the previous group matches successfully, then the matching of the yes-pattern is performed here
No-pattern: if the previous group match is not successful, that is, no matching group content is found, then the no-pattern is matched.
Note: the yes-pattern and no-pattern above are both normal regular expressions that match what you need.
3. Grammar
If there is a no-pattern, it should be preceded by a vertical bar'|'to separate yes-pattern from no-pattern
If you don't want to match the part of no-pattern, you can leave it unwritten along with'|'.
Example:
> re.search (r'(\ d +)? (? (1)\ W + | jb51\.\ W +), 'jb51.com') > re.search (r' (\ d +)? (? (1)\ W +)', '100jb51')
Where? (1) represents group with serial number 1, that is, above (\ d +), no-pattern is not used in the second expression.
4. Detailed explanation of usage
Again with the above examples, let's change them and explore their detailed usage and skills with examples.
4.1 remove the question mark after (\ d +), that is, there must be group 1, not optional
Execute:
> re.search (r'(\ d +) (? (1)\ w + | jb51\.\ w +)', 'jb51.com') >
The overall rule matching failed, the return was empty, and the no-pattern part did not execute as expected.
4.2 change the matching string > re.search (r'(\ d +) (? (1)\ w + | jb51\.\ w +)', '1jb51.com')
It can be matched. Both the group1 and yes parts are matched.
4.3 group optional cases (add question mark after group) > > re.search (r'(\ d +)? (? (1)\ w + | jb51\.\ W +)', 'jb51') >
If the group does not match and it is optional, then the group will be considered unsuccessful and continue to perform the no-pattern partial match
Only group 1 part is matched, and yes part does not match > re.search (r'(\ d +) (? (1)\ w + | jb51\.\ W +)', '1000')
Only the group 1 part is matched, and there is no match in the yes part, and there is also a matching result, which means that yes-pattern and no-pattern do not have to match successfully.
That's all for "how to understand Python3 regular expression (? (id/name) yes-pattern | no-pattern) conditional matching". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.