In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.