Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The 40 python regular expression match method matches the string to make

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Lesson 1: use the match method to match the string # regular expression: use the match method to match the string 'regular expression: is used to process text, abstracting a similar set of strings The resulting text pattern string windows dir *. Txt file1.txt file2.txt abc.txt test.doca-file1.txt-blinux/mac ls is mainly used to learn five aspects of regular expressions. 1. Match: detect whether a string matches a regular expression. 2. Search: search for substrings that match regular expressions in a long string. Findall: find strings 4. Sub and subn: search and replace 5 .split: specify delimiters through regular expressions Use this separator to split the import re # into the regular expression module's m = re.match ('hello',' hello') # the first string that specifies the regular expression The second string that represents the string to be matched can actually be a simple string print (m) # print (m. Regular expressions. Regular expressions _) # look at the type of m Matchprint (type (m)) # m = re.match ('hello') 'world') if m is not None: print (' match succeeded') else: print ('match not successful') # regular expression if the string to be matched is included If m = re.match ('hello',' world hello') print (m) # None does not match, the return value is None#. The prefix of the string to be matched can match the regular expression. The system also thinks it is a match m = re.match ('hello',' hello world') print (m) #-lesson 2 uses the search function to find a substring in a string the difference between the search function and the match function is The string in the search function can be matched, and the match function must be prefixed to match the regular expression. The system also thinks that import rem = re.match ('python',' I love python.') if m is not None: print (m.group ()) # the method to call group is returned to a group print (m) # Nonem = re.search ('python',' I love python.') if m is not None: print (m.group ()) print (m) # # python# span= (7) 13) indicates that python is in'I love python.' The position in the string is left closed and right open.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report