In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what is the difference between match () and search () in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
The match () function only detects whether the RE matches at the beginning of the string, and search () scans the entire string for a match, that is, match () returns only if the match is successful at position 0, and match () returns none if the start position is not matched successfully.
For example:
#! / usr/bin/env python#-*-coding=utf-8-*-import retext = 'pythontab'm = re.match (r "\ w +", text) if m: print m.group (0) else: print' not match'
The result is: pythontab
And:
#! / usr/bin/env python#-*-coding=utf-8-*-# import retext ='@ pythontab'm = re.match (r "\ w +", text) if m: print m.group (0) else: print 'not match'
The result is: not match
Search () scans the entire string and returns the first successful match
For example:
#! / usr/bin/env python#-*-coding=utf-8-*-# import retext = 'pythontab'm = re.search (r "\ w +", text) if m: print m.group (0) else: print' not match'
The result is: pythontab
How about this:
#! / usr/bin/env python#-*-coding=utf-8-*-# import retext ='@ pythontab'm = re.search (r "\ w +", text) if m: print m.group (0) else: print 'not match'
The result is: pythontab
Summary:
Regular expression match () function in Python
If we do not create pattern objects, we can use the match function to match regular expressions directly. In my opinion, this way is more concise, but it is not suitable for the writing of large programs, and later maintenance may be difficult, but writing some small scripts is perfectly competent.
Regular expression search () function in Python
Search function and match function are somewhat similar, both can match patterns, but match and search functions are also different, and there is a big difference. Match function can only match the beginning of the string, while search can match anywhere in the string, but it also returns the first matching pattern found. Let's use examples to understand the difference between the two.
So much for sharing the difference between match () and search () in Python. I hope the above content can help you and learn more. If you think the article is good, you can share it for more people to see.
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.