Iv. Regular,
RE module:Action: Object to: String,
Pre-Class Introduction:
Example One,
s='dsdsadsadadsalexdsds'
S.find (' Alex ')
How to find the inside of the string Alex;? Learn how to use the past: Find, split, replace. But the method of string built-in is very limited, and can only achieve exact match.
If you want to find: What's the need to start with a**? Can not be achieved. Fuzzy matching is used to achieve fuzzy matching.
Example two, fuzzy matching
# in the text there is a bunch of ID 4504231989347382822110423198934738282211042319923473828221104231995347382822 ... # demand: Find people from Beijing after 1990 years of origin
^110......1990+*
Find a bunch of numbers inside a string:
The regular is essentially a small, highly specialized programming language. (python) is embedded in Python and is implemented through the RE module. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.
Character matching: (normal character, metacharacters)
Ordinary characters: Most characters and letters will match themselves
Import Re Print (Re.findall ('Alex','dsfjdsfldsjfdalexdsds')) >>>['Alex']
Metacharacters:. ^ $ * +? {} [] | () \
Character Explanation:
1?? wildcard character '. '
Import Re Print (Re.findall ('A.. x','dsfjdsfldsjfdalexdsds'))>>>[' Alex']
2??、 Sharp Horn:^ with what begins. Match at the beginning of the string. If the match meets the requirements, the matching succeeds.
Import Re Print (Re.findall ('^d. J','dsfjdsfldsjfdalexdsds'))>>>[' dsfj']
3??、 The $ symbol ends with what,
Import Re Print (Re.findall ('d.. S $','dsfjdsfldsjfdalexdsds'))>>>>[ ' DSDS ']
Repeating symbols:
4??、 * (0,∞) d* to match the characters next to each other in Infinity → greedy matches,
ImportRePrint(Re.findall ('d*','Dsfjdsflddddddddddsjfdalexdsds'))>>>['D',"',"',"','D',"',"',"','dddddddddd',"',"',"','D',"',"',"',"','D',"','D',"',"']
Import Re Print (Re.findall ('^d*','dsfjdsflddddddddddsjfdalexdsds')) >>['d']
???、 + match [1,∞] least match one character trailing plus one? The number becomes an inert match.
ImportRePrint(Re.findall ('alex*','Dsfjdsfalesjfdsds'))ImportRePrint(Re.findall ('alex+','Dsfjdsflalesjfdsds'))>>>['Ale'][]
6??、? [0,1]
Import Re Print (Re.findall ('Alex? ','dsfjdsflalexxxsjfdsds'))>>>[' Alex']
7??、 {0,}=*:0} to infinity, {1,}=+: "1,+00", {0,1}=? , {6} repeated 6 times
Import Re Print (Re.findall ('alex{6}','dsfjdsflalexxxsjfdsds')) >>>[]
Add: Add:? It becomes an inert match, with the smallest to find a match.
ImportRePrint(Re.findall ('alex+?','Dsfjdsflalexxxsjfdsds'))ImportRePrint(Re.findall ('alex*?','Dsfjdsflalexxxsjfdsds'))>>>>['Alex']['Ale']
8??、 []: Character set. Any symbol placed inside is a normal symbol that matches one of the inside
Apart from
"A-Z" range, A to Z
Import Re Print (Re.findall ('x[a-z]*','xypsssdsdsdsdssxzp')) >>>['xypsssdsdsdsdssxzp']
"^a-z" meaning: non-matching non-A to Z letters
ImportRePrint(Re.findall ('www. [Oldboy Baidu]','Www.baid'))ImportRePrint(Re.findall ('X[yz]','Xyssssxz'))ImportRePrint(Re.findall ('x[yz]p','Xypssssxzp'))>>>['www.b']['XY','XZ']['Xyp','Xzp']
9??、
Day, module-basedir, OS, JSON module, Pickle, and regular module.