Python Basics of RE
There are a couple of ways to use the RE module mainly:
Re.match () # match a string from scratch Re.search () # Browse all strings to match the first rule-compliant string Re.findall () # place all matching items in a single list # Re.finditer () re.split () re.sub ( )
well, Let's briefly introduce the basics of the RE module:
1. meta-characters. ^ $ * +? {} [] | () \ ,
Features of the 2.\:
#1. Backslash followed by meta character removal of special functions
#2. a backslash followed by ordinary characters for special functions
#3. string matched by a character group that references an ordinal
With the basic concept above, you can write a few examples to illustrate the problem:
A='ABCD'. Find ('b') b='ABCD'. Split ('b') C='ABCD'. replace ('AB','er') d=re.findall ('ALEX.W','AAAALEXCW')#. For wildcards, a point can only represent one character. represents any character except the line breakE=re.findall ('^alex','ALEXAAALEXCW')#^ The angle character is used to control the beginning, and $ is used to control the end of theF =re.findall ('alex*','AAAALECW')#* Repeat 0~ multiple times for characters preceding the control * symbolG =re.findall ('alex+','AAAALEXXXXCW')#+ Repeat 1~ multiple times for the character preceding the control + symbolH=re.findall ('alex?','AAAALEXCW')#? Repeat 0~1 times for the characters in front of the control symboli = Re.findall ('alex{3,5}','AAAALEXXXXXXXCW')#{} repeats parenthesis input for characters preceding the {} symbolg = Re.findall ('a[bc]d','AAAACDSSSSSACDVVDABCDF')#[] represents the character set meaning that the relationship of the elements in the character set is "or"#in the character set [1-9] means to take 1 to 9 of these numbers are ok, [a-z] to take A to Z these letters are okay#[^xx] Sharp Angle ^ in the character set meaning is to take the character set in the "non" element
Here are some examples of the common methods above:
There are groups and no groupings in the match method#1. No groupingPrigin ='Hello Alex bcd alxe leg Alex ACD age19'R= Re.match ('h\w+', Prigin)Print(r.group ())#get all the results that match toPrint(r.groups ())#gets the grouped results that are matched in the modelPrint(r.groupdict ())#gets the grouped results that are matched in the model#2. There are groups#the goal is to extract the specified content that matches successfully (first match all the regular, and then the successful local content Extraction)Prigin ='Hello Alex bcd alxe leg Alex ACD age19'R= Re.match ('(h) \w+', Prigin)Print(r.group ())#gets all the results that match to, with or without grouping after parentheses have no effectPrint(r.groups ())#gets the grouped results that are matched in the model, after which the parentheses are Extracted.Print(r.groupdict ())#gets the grouped results that are matched in the model#in grouopdict () A key and a value are required to be used, as shown in the following examplePrigin ='Hello Alex bcd alxe leg Alex ACD age19'R= Re.match ('(? P<n1>h) (? P<n2>\w+)', Prigin)Print(r.group ())#gets all the results that match to, with or without grouping after parentheses have no effectPrint(r.groups ())#gets the grouped results that are matched in the model, after which the parentheses are Extracted.Print(r.groupdict ())#gets the grouped results that are matched in the model#groups and no groupings in the search method#1. No groupingPrigin='Hello Alex bcd alxe leg Alex ACD age19'R= Re.search ('a\w+', Prigin)Print(r.group ())#get all the results that match toPrint(r.groups ())#gets the grouped results that are matched in the modelPrint(r.groupdict ())#gets the grouped results that are matched in the model#2. There are groupsPrigin ='Hello Alex bcd alxe leg Alex ACD age19'R= Re.search ('a (\w+). * (? P<name>\d) $', Prigin)Print(r.group ())#get all the results that match toPrint(r.groups ())#gets the grouped results that are matched in the modelPrint(r.groupdict ())#gets the grouped results that are matched in the model" "" "#FindAll Basic methods, do all the things that are groups ()#1. When looking for a character, search for a character, when found to meet the requirements (equivalent to take the required), and then start from the next character to findR1 = Re.findall ('\d+\w\d+','A2b3c4d5')Print(r1)#2. Empty content can also matchr2 = Re.findall ("','A2b3c4d5')Print(r2)#grouping and no grouping in FindAll methodsPrigin='Hello Alex bcd alxe leg Alex ACD age'R1= Re.findall ('a\w+', Prigin)Print(r1) R2= Re.findall ('(a\w+)', Prigin)Print(r2) R3= Re.findall ('a (\w+)', Prigin)Print(r3) R4= Re.findall ('(a) (\w+)', Prigin)Print(r4) R5= Re.findall ('( a) (\w+ (e)) (x)', Prigin)#from left to right, from outside to insidePrint(r5)#what you can add to the Finditer iterative processPrigin ='Hello Alex bcd alxe leg Alex ACD age'R6= Re.finditer ('( a) (\w+ (e)) (? P<n1>x)', Prigin)#from left to right, from outside to insidePrint(r6) forIinchr6:Print(i,i.group (), i.groups (), i.groupdict ()) a='Alex'N= Re.findall ('( \w) (\w) (\w) (\w)', A)Print(n) N= Re.findall ('(\w) {4}', A)Print(n) N= Re.findall ('(\w) *'A#* Represents a number of 0~, then when the 0 will appear a control to supplementPrint(n) N= Re.findall ("', A)Print(n)" "" "#explanation of the Re.split method (purpose of Segmentation)Origin ='Hello Alex bcd alxe leg Alex ACD age'Origin.split ('a')Print(origin.split ('a'))#the use of separators in strings#No groupingn = re.split ('a\w+', Origin,1)#The third parameter means to split only once from the beginning, and this method does not preserve the segmented characters#have groupN1 = Re.split ('(a\w+)', Origin)#The parentheses on the split character indicate that you need not only to split this character, but also to retain this characterN2 = Re.split ('a (\w+)', Origin)Print(n1, N2)" "" "#re.sub is used for Replacement.Origin='1JHJHJHJ2HJHJHJK4HJHJHJKH5JKHJHJK6HKJK'N1= Re.sub ('\d+','DDD', origin,1) N2= Re.sub ('\d+','DDD', Origin) n3,count= Re.subn ('\d+','DDD', Origin)Print(n1)Print(n2)Print(n3,count)
Python Basic RE Module