Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Python regular expression

Shulou Source: shulou.com Published: 2022-06-02 09:24:31 10月03日 Update

This article mainly explains "how to achieve Python regular expression". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Python regular expression".

1. Regular expression _ matches single character # regular expression-matches single character import re "" lst = re.findall (regular expression, string) "" # (1) predefined character set #\ d matches numeric strvar = "sdjfklj234&* (& 1" lst = re.findall ("\ d", strvar) print (lst) #\ D matches non-numeric strvar = "YWERsdf78_&" lst = re.findall ("\ D") Strvar) print (lst) #\ w matches letters or numbers or underscores (matches in Chinese are supported in regular functions) strvar = "sadf234_ ^ & *% $^ $% Hello" lst = re.findall ("\ w", strvar) print (lst) #\ W matches non-alphanumeric or underscore strvar = "sadf234_ ^ & *% ^ $% Hello" lst = re.findall ("\ W") Strvar) print (lst) #\ s matches any blank character ("\ t\ n\ r) strvar ="\ r "lst = re.findall ("\ s ", strvar) print (lst) #\ S matches any non-white space character strvar ="\ r 123 blank * ("lst = re.findall ("\ S ") Strvar) print (lst) #\ nmatch a newline character strvar = "" Today's National Day holiday is over, the brothers are full of loads, playing very sleepy, do their best to adjust. "" lst = re.findall (r "\ n", strvar) print (lst) #\ t matches a tab lst = re.findall (r "\ t") Strvar) print (lst) # (2) character group [] matches the characters enumerated in the character group (choose one by default) # lst = re.findall ("[123c]", "a1b2c3d4") # print (lst) print (re.findall ('a [ABC] baud abb acb adb')) # aab abb acbprint (re.findall ('a [0123456789] b') 'a1b a2b a3b acb ayb')) # A1b A2b a3b# optimized writing 0123456789 = > 0-9print (re.findall ('a [0-9] bologna meme A1b a2b a3b acb ayb')) print (re.findall ('a [abcdefg] baked mummy a1b a2b a3b acb ayb adb')) # acb adb# optimized writing abcdefg = > AFI expresses all lowercase letters a-zprint (re.findall ('a [a-g] b') 'a1b a2b a3b acb ayb adb')) print (re.findall ('a [a-z] baud acb ayb adb')) # acb adb aybprint (re.findall ('a [ABCDEFG] baked memoir A1b a2b a3b aAb aDb aYb')) # aAb aDb# optimized writing ABCDEFG = > Amurg expresses all uppercase letters A-Zprint (re.findall ('a [A-G] b') 'a1b a2b a3b aAb aDb aYb') # aAb aDbprint (re.findall ('a [A-Z] baud aAb aDb aYb')) # aAb aDb aYb# Note: the intermediate ascii code of Amurz contains a special symbol print ('a [A-z] b'). 'a1b a2b a3b acb ayb adb a [b') # matches all letters and numbers print (re.findall ('a [a-zA-Z0-9] bchapter acb ayb adb a [b')) print (re.findall ('a [0-9a-zA-Z]] acb ayb adb b aab aAb aWb aqba1b')) # aab aAb aWb aqb a1bprint (re.findall ('a [0-9] [* # /] b') )) # a1/b# ^ appears at the beginning of the character group except. Print (re.findall ('a [^-+ * /] baked, "a% b ccaa*bda&bd") # a% b a&b# matches special characters such as ^ -\ need to be preceded by\ escape strvar = "a ^ c Amurc a\ c" lst = re.findall (r "a [\ ^\ -\] c", strvar) print (lst) print (lst [- 1]) # Note: in order to prevent escape In the regular expression or in the string to be matched, add r to match strvar = r "a\ b" lst = re.findall (r "a\\ b", strvar) print (lst [0]) 2. Regular expression _ matches multiple characters # regular expression-matches multiple characters # (1) quantifier import re'''1)? Match 0 or 1 a 'print (re.findall (' astatabbzab abb aab') # abb ab abb ab'''2) + match 1 or more a 'print (re.findall (' a'ab aaaaaab abb')) # ab aaaaaab ab'''3) * match 0 or more a 'print (re.findall (' a''print') 'b ab aaaaaab abbbbbbb')) # b ab aaaaaab abbbbbbb' 4) {mQuery n} match m to n a' # 1

Tags: Characters content expressions regular letters parentheses numbers strings grouping greed results underscores representatives Liu Lao big brother uncle symbols boundaries multiplication and division writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB NVidia Microsoft MySQL