0x01 common meta-characters and special symbols
1 . Matches any character except newline characters, excluding line breaks2 #F.O can match FAO, FOO, etc.3 4^match the start of a string5 #^from matches any string starting with from6 7 $ match end of string8 #/bin/tcsh$ matches any string ending with/bin/tcsh9 #^/bin/tcsh$ matches a string consisting only of/bin/tcshTen One [] is used to match a specified character type A #b[aeiu]t matches bat, bet, bit, but - -[^...] does not match any one of the characters that appear in the character set the ? Repeat 0 to 1 times for the previous character -*repeat 0 to several times for the previous character - {m} repeats the previous character m times - {m, n} repeats M to n times the previous character + () matches the regular expression in parentheses and saves it as a sub-group - \d Matching numbers + \d matches any non-digital A \s matches any whitespace character at \s matches any non-whitespace character - \w matches any alphanumeric character - \w matches any non-alphanumeric character - \b Match the beginning or end of a word (boundary) - \nn matching a saved child group - \c matches the special character C (that is, cancels its special meaning, literally matches) in\a (\z) matches the start of the string (end)
0x02 using the closure operator (*, +,?, {}) to implement multiple occurrences/repeat matches
1 [Dn]ot? # Do , no, dot, not 2 0? [1-9] # any number in the 1~9, the front may have a 0 3 [0-9]{15,16} # matches 15-16 digits 4 </?[ ^>]+> # matches any legitimate HTML tag 5 \w+-\d+ # A string of letters or numbers and at least one number, in the middle-connected 6 ...
0x03 Common methods
1.compile (pattern, flags=0) compiles regular expression pattern patterns, flags is an optional identifier, and returns a Regex object
1 Import Re 2 ' Hello World ' 3 >>> r = re.compile (R'\w*e\w*') # matches words containing E 4 >>> r.findall (test)5 ['hello']
2.match (Pattern, string, flags=0) attempts to match strings with regular expression pattern patterns String,flags is an optional identifier, if the match succeeds, returns a matching object, otherwise none
1 >>> r = Re.match (R'\[email protected]\w+\.com','[email Protected]')2print r.group (0)3 [email protected]
3.search (Pattern, string, flags=0) searches the string for the first occurrence of a regular expression pattern, flags is an optional identifier, and if the match succeeds, it returns The first match object , otherwise none is returned
The use of Re.match () and Re.search () is the same, except that the Re.match () value matches the beginning of the string and returns none if the corresponding string is not matched at the beginning. Re.search () searches the string until it finds a matching string and returns.
4.findall (Pattern, string [, flags]) searches the string for a regular expression pattern all (non-duplicates) of pattern patterns, returning a list of matching objects
1 >>> re.findall (R'\w*o\w*'hotHello foofoo' )2 ['hot''Hello' foo'foo']
5.Sub(pattern, repl, string, count=0, flags=0) replaces all occurrences of the regular expression pattern in a string with the string repl, if the value of count is not given, replaces all matching places
1>>> Re.sub (r'X','Mr.smith','attn:x, Dear X,')2 'Attn:Mr.Smith, Dear Mr.smith,'3>>> Re.sub (r'X','Mr.smith','attn:x, Dear X,', Count=1) 4 'Attn:Mr.Smith, Dear X,'5>>>
6.split (Pattern, string, max=0) splits the character string into a list based on the delimiter in the regular expression pattern, returning the list of successful matches, dividing the Max times by the default, by dividing all matching places.
1 >>> re.split (R':'str1:str2:str3') 2 ['str1'str2 ' STR3']
Used to split the results of the WHO command output
[Email protected]:~> who >who.txtxt:02015-01-25 09:34(: 0) XT pts/1 2015-01-26 13:41(: 0) XT pts/2 2015-01-27 15:05(: 0) XT pts/3 2015-01-27 15:05(: 0) XT pts/4 2015-01-27 15:05(: 0)#who.py#!/usr/bin/env python#Coding=utf-8Importref= Open ('Who.txt','R') forEachlineinchf.readlines ():PrintRe.split ('\s\s+|\t', Eachline.strip ()) F.close () e#Output['XT',': 0','2015-01-25 09:34 (: 0)']['XT','PTS/1','2015-01-26 13:41 (: 0)']['XT','PTS/2','2015-01-27 15:06 (: 0)']['XT','PTS/3','2015-01-27 15:06 (: 0)']['XT','PTS/4','2015-01-27 15:06 (: 0)']
Python RE Module