Python learns Regular Expressions and python regular expressions.
I. re Module
1. re. match # match from the starting position. If match () is not started, none is returned.
Syntax: re. match (pattern, string, flags = 0)
Pattern |
Matched Regular Expression |
String |
The string to be matched. |
Flags |
A flag, used to control the matching mode of regular expressions, such as case-sensitive or multi-row matching. |
2. re. search # search the entire string
Syntax: re. search (pattern, string, flags = 0)
Pattern |
Matched Regular Expression |
String |
The string to be matched. |
Flags |
A flag, used to control the matching mode of regular expressions, such as case-sensitive or multi-row matching. |
3. re. findall # search the entire string and return a list
Syntax: re. findall (pattern, string [, flags]):
Flags: Parameters
Re. I |
Make matching case insensitive |
Re. L |
Perform locale-aware matching |
Re. M |
Multi-row matching, affecting ^ and $ |
Re. S |
Make. Match All characters including line breaks |
Re. U |
Parses Characters Based on the Unicode Character Set. This flag affects \ w, \ W, \ B, \ B. |
Re. X |
This flag gives you a more flexible format so that you can write regular expressions more easily. |
#-*-Config = UTF-8-*-# Regular Expression #1. Processing text and data #2. It is a logical formula for string operations. import re; # Python supports regular expressions through the re module. The general step to Use re is to first compile the string form of the regular expression into a Pattern instance, # Then use the Pattern instance to process the text and obtain the matching result (a Match instance ), finally, use the Match instance to obtain information and perform other operations. Pattern = re. compile (r "hello"); match = pattern. match ("hello Word"); if match: print (match. group () ;#=============================. match any character (except \ n) ========================= ma = re. match (r "a", "a"); # you only want to match aprint (ma. group (); # returns a. If the matching fails, it is null. ma1 = re. match (r ". "," d "); print (ma1.group (); # Return dma2 = re. match (r "{.} "," {c} "); # match any character in braces (except \ n) print (ma2.group ()); #============================== [...] match any character set ============================ ma3 = re. match (r "{[abc]} "," {A} "); # match any character print (ma3.group () in the braces abc; ma4 = re. match (r "[a-z]", "B"); # match print (ma4.group (); ma5 = re. match (r "[a-zA-Z]", "F"); # match any a-z character print (ma5.group (); ma6 = re. match (r "[a-zA-Z0-9]", "8"); # match any letter with the digit print (ma6.group ()); #==============================\w matching any word character ===================== = ma7 = re. match (r "[\ w]", "4"); # print (ma7.group ()); #=====================================\ W any non-word characters ================== =============== ma8 = re. match (r "[\ W]", "*"); Print (ma8.group ()); #==================================\d matching numbers ====================== === ma9 = re. match (r "[\ d]", "3"); # equals to [0-9] print (ma9.group ()); #=====================================\ D = =========== ma9 = re. match (r "[\ D]", "("); print (ma9.group ()); #========================================\s matching Spaces =========== ma10 = re. match (r "[\ s]", ""); print (ma10.group ()); #=========================================\ S matching non-space ================== ============ ma11 = re. match (r "[\ S]", "Sa"); print (ma11.group ()); #========================================\escape =============== =============== ma12 = re. match (r "\ [\ w] \]", "[2]"); # match any character in brackets print (ma12.group ()) #================================##-*-config = UTF-8 -*- import re; #=========================== * the first character of the horse is 0 or unlimited ============ ma = re. match (r "[A-Z] [a-z]", "Aa"); print (ma. group (); # Aama1 = re. match (r "[A-Z] [a-z] *", "Fdsdasd22"); # [a-z] unlimited print (ma1.group ()); # Fdsdasd #============================ + Match the previous character once or infinitely ============== ma2 = re. match (r "[_ a-zA-Z] + [_ \ w] *", "_ dasd "); # print (ma2.group (); #===================? Match the first character 0 times or 1 time ========================== ma3 = re. match (r "[1-9]? [0-9] "," 10 "); # matching a positive and double digit includes 0 print (ma3.group (); ma4 = re. match (r" [1-9]? [0-9] "," 08 "); # print (ma4.group ()); #0 # ================================={ m} ========================== ma5 = re. match (r "[0-9] {6}", "1313123"); # match 0-9 any character 6 times print (ma5.group ()); #131312 #==================================={ m, n} match the previous character m-n times ============================ ma6 = re. match (r "[a-zA-Z0-9] {163} @ 163.com"," jalja365@163.com "); # match mailbox print (ma6.group ()); #======================= *? +? ?? Match as few as possible =============================== ma7 = re. match (r "[0-9] [a-z] *", "2we"); print (ma7.group (); #2wema8 = re. match (r "[0-9] [a-z] *? "," 2we "); print (ma8.group (); #2ma9 = re. match (r" [0-9] [a-z] +? "," 2we "); print (ma9.group (); #2wma10 = re. match (r" [0-9] [a-z]? "," 2we "); print (ma10.group ()); #2 w #================================================= ====#-*-config = UTF-8-*-import re; #================================ search (pattern, String, flags = 0) search for matching in a string ================================ str1 = "jalja_365-1321"; ma = re. search (r "\ d +", str1); # obtain the print (ma. group (); #365 #======================= findall (pattern, String, flags = 0) returns the list of all matched parts ============================ str2 = "java = 90, python = 99, c = 300 "; ma2 = re. findall (r "\ d +", str2); print (ma2); # ['90', '99 ', '20140901'] returns num = sum ([int (x) for x in ma2]) as a list of all numbers. # Calculate the sum of all elements in the list #===================== sub () replace the character matching the regular expression in the string with the new string ======================== str3 = "java = 99 "; ma3 = re. sub (r "\ d +", "100", str3); print (ma3); # java = 100 # Use the def add_1 (match) function ): # match is the first parameter val = match of the macth object, that is, sub. group (); print (val); return str (int (val) + 1); ma4 = re. sub (r "\ d +", add_1, str3); print (ma4 ); # java = 100 #=================== split () returns the list of strings separated by matching rules ========================== str4 = "jalja: c ++ java Python js, c # "; ma5 = re. split (r ": |,", str4); print (ma5); # ['jalja ', 'C', 'c ++', 'java ', 'python', 'js', 'c # ']