Regular expressions are special characters sequences that help you easily check whether a string matches a certain 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 regular expression functions.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. This object has a series of methods for regular expression matching and replacement.
The re module also provides functions that are exactly consistent with the functions of these methods, which use a pattern string as their first argument.
The use of python regular expressions (experimental code), the specific code is as follows:
import re data='''12345 2019-05-20 13:30:04,102 E:/PythonProject/accountReport-20190520/createReport_20190520.py(164): [INFO]start=24h-ago&m=sum:{compared=week,redis=6380,endpoint=192.168.8.11_Redis-b} 2019-05-20 13:30:04,133 E:/PythonProject/accountReport-20190520/createReport_20190520.py(164): [INFO]start=24h-ago&m=sum:keys{redis=6380,endpoint=192.168.8.120_Redis-sac-a} ''' # 1.1) Use of non-compiled regular expressionsdef re_nocompile(): pattern="report" #Match time format r=(pattern,data,flags=) # findall method Returns string print(r) # 1.2) Use of compiled regular expressions (high efficiency)def re_compile(): pattern = "[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}" # Match time format re_obj=(pattern) #Create an object r=re_obj.findall(data) # findall method Returns string print(r) # 2.1) "Match" class functions use findall, match, search, finditerdef re_match(): pattern = "\d+" # Match numbers r=(pattern,data) The #match function is the beginning of the matching string, similar to startwith if r: # After successful match using match, return an object of type SRE_MATCH, which contains the relevant pattern and the original string, including the start position and the end position print(r) print(()) print(()) print() print(()) # group() is used to propose a string intercepted by a group. group() and group(0) match the overall result of regular expressions. # group(1) lists the first bracket matching part, group(2) lists the second bracket matching part, and group(3) lists the third bracket matching part. # Of course, there are no brackets in the regular expression, group(1) is definitely wrong print() else: # If match does not match, return None print("False") def re_search(): pattern = "[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}" # Match time format r=(pattern,data) # The search method is a match for all positions, and returns the SRE_MATCH object print(r) print(()) #First Location print(()) #End Location # finder returns an iteratordef re_finditer(): pattern = "\d+" # Match numbers r=(pattern,data) for i in r: print(()) # Greedy Match: Always match the longest string (default)# Non-greedy matching: Always match the shortest string (add it? to implement it when matching the string)def re_find02(): r1=("Python.*\.",data) # Greedy Match print(r1) r2 = ("Python.*?\.", data) #Non-greedy match print(r2) if __name__=="__main__": re_nocompile() re_compile() re_match() re_search() re_finditer() re_find02() import re import requests data='''12345 2019-05-20 13:30:04,102 E:/PythonProject/accountReport-20190520/createReport_20190520.py(164): [INFO]start=24h-ago&m=sum:{compared=week,redis=6380,endpoint=192.168.8.11_Redis-b} 2019-05-20 13:30:04,133 E:/PythonProject/accountReport-20190520/createReport_20190520.py(164): [INFO]start=24h-ago&m=sum:keys{redis=6380,endpoint=192.168.8.120_Redis-sac-a} ''' # 1) "Modify Class" function# 1.1) The sub function matches and replaces it, returning the replaced stringdef re_sub(): pattern = "[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}" # Match time format data01=(pattern,"timeString",data) print(data01) # 1.2) Splite splits the string into a substring list, and can specify multiple separators at the same timedef re_split(): r=(r"[:\-\=]",("'")) print(r) # 2) An example of matching htmldef re_html(): r=("https:///") print() try: web=("(https:.*?.com)",str()) print(web) except Exception as err: print(err) if __name__=="__main__": re_sub() re_split() re_html()
Summarize
The above is the use of python regular expressions (experimental code) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!