Today's content:
Re module
Collentions Module
Regular expressions
1.re module:
The common methods under the RE module are:
1 ImportRe2 #1.re.findall ()3Ret1 = Re.findall ('a','Eva Egon Yuan')#take ' a ' for the eye and find all. and returned as a list. 4 Print(RET1)5 6 #2.re.search ()7Ret2 = Re.search ('a','Eva Egon Yuan'). Group ()#match ' A ' from string8 Print(Ret2)9 Ten #3.re.match () OneRet3 = Re.match ('a','ABC'). Group ()#match ' a ' A Print(RET3) - - #4.re.split () theRet4 = Re.split ('[AB]','ZXCABCD')#split with ' AB ' - Print(RET4) - -5. Re.sub (): Change the number to ' H ' +Ret5 = Re.sub ('\d','H','Eva3egon4yuan4') - Print(RET5) + A6. Matching numbers atobj = Re.compile ('\d{3}') -Ret6 = Obj.search ('abc123eee') - Print(Ret6.group ()) - -7. Returns an iterator -Ret7 = Re.finditer ('\d','ds3sy4784a') in Print(Next (RET7). Group ()) - Print(Next (RET7). Group ()) to Print([I.group () forIinchRet7]) + - #priority query for 8.findall theRet8 = Re.findall ('www. (oldboy|baidu). com','www.baidu.com') * Print(RET8) $ Panax NotoginsengRet8 = Re.findall ('www. (?: baidu|oldboy). com','www.oldboy.com') - Print(RET8) the + #Priority of 9.split ARet9 = Re.split ('\d+','Eva3egon4yuan') the Print(RET9) + -Ret9 = Re.split ('(\d+)','Eva3egon4yuan') $ Print(RET9) $ - - Output Result: the['a','a'] - aWuyi a the['Zxc',"','CD'] - Evahegonhyuanh Wu123 -3 About4 $['7','8','4'] -['Baidu'] -['www.oldboy.com'] -['Eva','Egon','Yuan'] A['Eva','3','Egon','4','Yuan']
View Code
1 FindAll Priority query:
Import Reret = Re.findall (' www. ( baidu|oldboy). com ', ' www.oldboy.com ') print (ret) # [' Oldboy '] this is because FindAll will prioritize the contents of the matching result set, and if you want to match the results, Cancel permission to ret = Re.findall (' www. (?: baidu|oldboy). com ', ' www.oldboy.com ') print (ret) # [' Www.oldboy.com ']
2 Split's priority query:
Ret=re.split ("\d+", "Eva3egon4yuan") print (ret) #结果: [' Eva ', ' Egon ', ' Yuan ']ret=re.split ("(\d+)", "Eva3egon4yuan") Print (ret) #结果: [' Eva ', ' 3 ', ' Egon ', ' 4 ', ' Yuan '] after #在匹配部分加上 () The result is different, the #没有 () does not retain the matching items, but there is () can retain the matching items, # This is very important in some of the use processes that need to keep the matching part.
2.collections module:
On the basis of the built-in data type (DICT, list, set, tuple),The collections module also provides several additional data types: Counter, deque, Defaultdict, Namedtuple, and Ordereddict.
1.namedtuple: Generate a tuple that can access the content of an element by using a name
2.deque: Double-ended queue to quickly append and eject objects from the other side
3.Counter: Counter, mainly used to count
4.OrderedDict: Ordered Dictionary
5.defaultdict: Dictionary with default values
(1). namedtuple
From collections Import Namedtuplepoint = Namedtuple (' point ', [' X ', ' y ']) print (p) Results: Point (x=1, y=2)
(2). Deque:
From collections Import Dequeq = Deque ([' A ', ' B ', ' C ']) q.append (' x ') q.appendleft (' Y ') print (q) Result: Deque ([' Y ', ' A ', ' B ', ' C ', ' X '])
(3). Counter:
c = Counter (' Abcdeabcdabcaba ') print C output: Counter ({' A ': 5, ' B ': 4, ' C ': 3, ' d ': 2, ' E ': 1})
(4). Orderedict:
From collections Import ORDEREDDICTD = ([' A ', 1), (' B ', 2), (' C ', 3), (' Zxc ', 123)]) print (d) o_d = Ordereddict ([' A ', ' 1 '), (' B ' , 2), (' C ', 3)]) print (o_d) results: [(' A ', ' 1 '), (' B ', 2), (' C ', 3), (' Zxc ', 123)]ordereddict (' (' A ', 1 '), (' B ', 2), (' C ', 3)])
(5). Defaultdict
From collections Import Defaultdictvalues = [11,22,33,44,55,66,77,88,99,90]my_dict = defaultdict (list) as value in Values: if value > my_dict[: K1 '].append (value) else: my_dict[' K2 '].append (value) print (My_ dict) Results: defaultdict (<class ' list '), {' K2 ': [One, one, one, one, one, one, one], ' K1 ': [77, 88, 99, 90]})
3. Regular Expressions:
Say the rules I already know you are dizzy, now let us first look at some practical applications. Online test Tool http://tool.chinaz.com/regex/
The first thing you need to know is that when it comes to the regular, it's only related to strings. In the tool I have given you, every word you enter is a string.
Second, if a value in a position does not change, then no rules are required.
For example you want to use "1" to match "1", or "2" to Match "2", directly can match on. This can be done easily with Python's string operations.
Then we'll consider more of the range of characters that can appear in the same position.
character Group: [Character Group] the various characters that may appear in the same position make up a group of characters, and in regular expressions the characters are divided into classes, such as numbers, letters, punctuation, and so on. If you now ask for a position " only one number can appear ", then the character in this position can only be one of the 10 numbers, 0, 1, 2...9.
View Code
Meta-character: . Matches any character except newline characters. \w matches letters or numbers or underscores or kanji. \s matches any whitespace character \d match a number \ n matches a newline character \ t matches a tab \b matches the end of a word ^ matches the beginning of a string matching string \w matches non-alphabetic and numeric or underscore or kanji. \d Match Non- white-\s match non -whitespace | b matches the character A or character B () matches the expression in parentheses also means a group [] matches the characters in the string [ ^ ] matches all characters except characters in the character group. quantifier: * repeat 0 or more times + Repeat one or more times ? Repeat 0 or more times {n} repeats n times {n,} repeats n or more times {n,m} repeats n to M
View Code
Several commonly used non-greedy matching pattern:
*? Repeat any number of times, but with as few repetitions as possible +? Repeat 1 or more times, but repeat as little as possible?? Repeat 0 or 1 times, but repeat {n,m} as little as possible. Repeat N to M times, but repeat {n,} as little as possible. Repeat more than n times, but repeat as little as possible
. *? usage:
. Is any character * to take 0 to infinity length? Non-greedy mode. Where together is to take as little as possible any character, generally not so alone, he mostly used in:. *?x is to take a character of any length before the x appears
Python = = "module and regular expression