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

How to use regular expressions in python

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

Share

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

This article shows you how to use regular expressions in python. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

A regular expression is a special sequence of characters that helps you easily check whether a string matches a pattern.

Python has added the re module since version 1.5, which provides Perl-style regular expression patterns.

The re module enables the Python language to have all the regular expression functions.

The compile function generates a regular expression object based on a pattern string and optional flag parameters. The object has a series of methods for regular expression matching and replacement.

The re module also provides functions that are exactly the same as those of these methods, which take a pattern string as their first argument.

The use of python regular expressions (experimental code), as follows:

Import redata='''12345 2019-05-20 13 INFO 30 start=24h-ago&m=sum:zscore.keys {compared=week,redis=6380,endpoint=192.168.8.11_Redis-b} 2019-05-20 13 INFO 30 compared=week,redis=6380,endpoint=192.168.8.11_Redis-b 04133 E:/PythonProject/accountReport-20190520/createReport_20190520.py: [INFO] start=24h-ago&m=sum:keys {redis=6380 Use of uncompiled regular expressions def re_nocompile (): pattern= "report" # match time format r=re.findall (pattern,data) Flags=re.IGNORECASE) # findall method returns the string print (r) # 1.2) use of compiled regular expressions (efficient) def re_compile (): pattern = "[0-9] {1 findall 2}\: [0-9] {1 shot 2}\: [0-9] {1 shot 2} # matching time format re_obj=re.compile (pattern) # create an object r=re_obj.findall (data) # findall method returns Return string print (r) # 2. 1) the use of "match" class functions findall, Match, search, finditerdef re_match (): pattern = "\ d +" # match numeric r=re.match (pattern Data) the # match function is the beginning of the matching string Similar to startwith if r: # after a successful match using match, an object of type SRE_MATCH is returned, which contains the related pattern and the original string, including the start position and end position print (r) print (r.start ()) print (r.end ()) print (r.string) print (r.group ()) # group () used to propose the packet intercepted string. Group () and group (0) match the overall result of the regular expression. # group (1) lists the first parenthesis matching part, group (2) lists the second parenthesis matching part, and group (3) lists the third parenthesis matching part. # of course there are no parentheses in the regular expression, and group (1) must be wrong. Print (r.re) else: # match if it doesn't match Return None print ("False") def re_search (): pattern = "[0-9] {1Magne2}\: [0-9] {1Power2}\: [0-9] {1Power2}" # matching time format r=re.search (pattern,data) # search method is a match of all positions Returns the SRE_MATCH object print (r) print (r.start ()) # start position print (r.end ()) # end position # finditer returns an iterator def re_finditer (): pattern = "\ d +" # matching digit r=re.finditer (pattern Data) for i in r: print (i.group ()) # greedy match: always match the longest string (default) # non-greedy match: always match the shortest string (add? To implement) def re_find02 (): r1=re.findall ("Python.*\.", data) # greedy matching print (R1) R2 = re.findall ("Python.*?\." Data) # non-greedy matching print (R2) if _ _ name__== "_ _ main__": re_nocompile () re_compile () re_match () re_search () re_finditer () re_find02 () import reimport requestsdata='''12345 2019-05-20 13 30 re_find02 E:/PythonProject/accountReport-20190520/createReport_20190520.py: [INFO] start=24h-ago&m=sum:zscore.keys {compared=week,redis=6380 Endpoint=192.168.8.11_Redis-b} 2019-05-20 13 INFO 30 INFO 04133 E:/PythonProject/accountReport-20190520/createReport_20190520.py: [INFO] start=24h-ago&m=sum:keys {redis=6380,endpoint=192.168.8.120_Redis-sac-a}''# 1) "modified class" function # 1.1) sub function to match and replace Returns the replaced string def re_sub (): pattern = "[0-9] {1Magne2}\: [0-9] {1pc2}\: [0-9] {1pc2}" # matching time format data01=re.sub (pattern, "timeString", data) print (data01) # 1.2) splite splits the string into a list of substrings You can specify multiple delimiters def re_split (): r=re.split (r "[:\ -\ =]", data.strip ("'")) print (r) # 2) to match an example of html def re_html (): r=requests.get ("https://www.hao123.com/") print (r.content) try: web=re.findall (" (https:.*?.com) ") Str (r.content) print (web) except Exception as err: print (err) if _ _ name__== "_ _ main__": re_sub () re_split () re_html () the above is how to use regular expressions in python Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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