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 Python regular function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Python regular functions". In daily operation, I believe many people have doubts about how to use Python regular functions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python regular functions". Next, please follow the editor to study!

1. Reverse reference _ naming grouping # back reference import restrvar = "it's time to rest tomorrow" obj = re.search ((. *?) " Strvar) print (obj) # get the matching content res1 = obj.group () print (res1) # get the content in the group res2 = obj.groups () print (res2) # reverse reference syntax\ 1 refer to the content in the first parenthesis obj = re.search (r "(. *?)" Strvar) print (obj) print (obj.group ()) print (obj.groups ()) strvar = "z3d4pzd a1b2cab" obj = re.search (r "(. *?)\ d (. *?)\ d (. *?)\ 1\ 2" Strvar) print (obj) print (obj.group ()) print (obj.groups ()) # naming group "" 3) (? P regular expression) give this group a name 4) (? P = group name) refers to the name of the previous group. Put the matching content of the group name in the current location "" # write strvar = "z3d4pzd a1b2cab" obj = re.search (r "(? P.C.?)\ d (? P.C.?)\ d (? P.C.))\ 1\ 2" Strvar) print (obj) print (obj.group ()) # two strvars = "z3d4pzd a1b2cab" obj = re.search (r "(?))\ d ((?)?) (P=tag1) (? P=tag2)", strvar) print (obj) print (obj.group ()) 2. Regular function # regular function import re# search returns the first object through regular matching Fetch the value strvar = "3 / 4 / 6 / 4" obj = re.search (r "(\ d + [+ *]\ d +), strvar) print (obj) # get the matching content print (obj.group ()) # get the content in the packet (return tuple) print (obj.groups ()) # match verify user input (understand)"search in front of the regular expression is equivalent to match Other uses are exactly the same "" strvar = "a17366668888" strvar = "17366668888" # obj = re.search (r "^\ d +", strvar) # obj = re.match (r "\ d +", strvar) # print (obj.group ()) print (obj) # split cutting strvar = "alex | wusir_xboyww@risky" lst = re.split ("[| | @]", strvar) print (lst) strvar = "alex2341273894wusir234234xboyww11111risky" lst = re.split ("\ d +") Strvar) print (lst) # sub replacement strvar = "alex | wusir_xboyww@risky" strvar = strvar.replace ("| |", "&") strvar = strvar.replace ("_", "&") strvar = strvar.replace ("@", "&") print (strvar) "# sub (regular, replaced character, original string [, number of substitutions]) res = re.sub (" [| _ @] "," & ") Strvar) res = re.sub ("[| _ @]", "&", strvar,1) print (res) # subn replacement (same as sub, but with different returned values) res = re.subn ("[| _ @]", "&", strvar) res = re.subn ("[| _ @]", "&", strvar,2) print (res) # res = re.sub ("[| _ @]", "&") Strvar) # ('alex&wusir&xboyww@risky', 2) # finditer matches the corresponding contents in the string. The iterator "" returns the iterator. The iterator contains the object .group to get the matching value "" from collections import Iterator, Iterablestrvar = "sdf23647fdgdfg () * () * 23423423" it = re.finditer ("\ d +", strvar) print (isinstance (it). Iterator) for obj in it: print (obj.group ()) # compile specifies a uniform matching rule normally regular expressions are compiled once and executed once in order to avoid repeated compilation and save time and space, you can use compile uniform rules to compile once Lifetime benefit "" strvar = "asdfs234sdf234" pattern = re.compile ("\ d +") print ("") obj = pattern.search (strvar) print (obj.group ()) lst = pattern.findall (strvar) print (lst) # modifier # re.I makes the match case insensitive strvar = "subtitle" pattern = re.compile ("(. *?)" Flags=re.I) obj = pattern.search (strvar) print (obj.group ()) # re.M enables each line to be matched individually (multiple lines match) Affects ^ and $"" single-line independent matching, rather than overall matching "" strvar = ""

one hundred and eleven

222333 "" pattern = re.compile ("^ (?:. *?) $", flags=re.M) lst = pattern.findall (strvar) print (lst) # re.S makes. Match all characters, including line breaks, strvar = "" givesdfsdfmefive "more than # modifiers are used together through | splicing pattern = re.compile (". *? mefive ", flags = re.S | re.I | re.M) obj = pattern.search (strvar) print (obj.group ()) Tip: crawlers use finditer when crawling data, the data is too large, use an iterator to store strvar ="

one hundred and eleven

222333 "" pattern = re.compile ("^ (?:. *?) $") lst = pattern.findall (strvar) print (lst) # the result here is [] because. Does not match the newline character, so will not return the result pattern = re.compile ("^ (?:. *) $", flags=re.M). Here is the match of one line of a line. Can't use for i in to find ideas. *? Looking for ideas so far, the study of "how to use Python regular functions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report