Complete RegEx expressions (python) and regular expressions (python)
Upload images to show your innocence.
Regular (Regular Expression)
In the use of python, regular expressions are very important. It is essential for crawlers or certain judgments. Next, let's talk about regular expressions. Hope you can give me more advice.
1 import re2 result=re.match("abc",'abc.cm')3 print(result.group())
In this case, re import and split the line to start to enter the topic.
========================================================== ======================================
The first is the single-character match (character) of the regular expression)
Example:
1 >>> a=re.match(".","a")2 >>> print(a.group())3 a
1 >>> print(re.match("[bB]","beautiful").group())2 b3 >>> print(re.match("[bB]","Beautiful").group())4 B
1 >>> print (re. match ("today's day \ d", "Today's Day 1"). group () 2 today's day 13 >>>
========================================================== ======================================
Original string, that is, r
In a python regular expression, '\' is an escape character (it is special to add '\' before a letter or digit? For example, '\ n' line feed). Therefore, there is a question about how to match' \ '. if the language is used for expression, then four backslashes are required. The first two and the last two are converted into a backslash, and then merged into a backslash. (It seems interesting, but troublesome), So Native strings are derived to solve this problem.
1 >>> import re2 >>> a='c:\\a\\b\\c'3 >>> b=re.match(r'c:\\a',a).group()4 >>> print(b)5 c:\a
========================================================== ======================================
Match multiple characters (Quantity)
Example:
1 >>> print (re. match ("[A-Z] [a-z] *", "Dddfefdfd "). group () 2 Dddfefdfd3> match the first letter in upper case, the second letter in the upper case, and the subsequent or non-lower case letters.
1 >>> print (re. match ("[A-Za-z _] + [\ w _] *", "hello1123 "). group () 2 helloapps33 >>> match
1 >>> print (re. match ("[1-9]? [0-9] "," 98 "). group () 2 983 >>> match 0-99
1 >>> print (re. match ("[A-Za-z0-9 _] {8, 20}", "hellodfejkjafejls1123"). group () 2 hellodfejkjafejls1123 >>> match 8-20 characters
========================================================== ======================================
Matching character Boundary Problem (Boundary)
Example:
1 >>> print (re. match ("[\ w] {163 \. com $ "," dfefsfakdnf@163.com "). group () 2 dfefsfakdnf@163.com3 >>> $ match ends
1 >>> print(re.match("[\w]{4,20}@163\.com$","dfefsfakdnf@163.com222").group())2 Traceback (most recent call last):3 File "<stdin>", line 1, in <module>4 AttributeError: 'NoneType' object has no attribute 'group'5 >>>
1 >>> print (re. match (r ". * \ buti \ B", "bea utiful"). group () 2 bea uti3 >>> letter Boundary
========================================================== ======================================
Grouping of Regular Expressions
Example:
1 >>> print (re. match ("[1-9]? \ D $ | 100 "," 100 "). group () 2 1003 >>> print (re. match (" [1-9]? \ D $ | 100 "," 98 "). group () 4 985 >>> | group
1 >>> print (re. match ("\ w {163} @ (126 | qq )\. com "," adbc@qq.com "). group () 2 adbc@qq.com3> print (re. match ("\ w {163} @ (126 | qq )\. com "," adbc@163.com "). group () 4 adbc@163.com5 >>> () group match 163, 126, QQ mailbox
1 >>> print (re. match (r "<([a-zA-Z] *)> \ w * </\ 1>", "
1 >>> print (re. match (r "<(? P <name1> \ w *)> <(? P <name2> \ w *)>. * </(? P = name2)> </(? P = name1)> ","
========================================================== ======================================
Advanced re usage
Search
1 >>> print (re. search (r "\ d +", "23455 meals today"). group () 2 234553 >>> change match to search
Findall
1 >>> print (re. findall (r "\ d +", "I had 23455 meals today, ran 35666 kilometers, and took 313 steps") 2 ['123', '123 ', '20140901'] 3 >>> return list
Sub
1 >>> a = re. sub (r "\ d +", "9999", "python = 123") 2 >>> print (a) 3 python = 99994 >>> replace
Split
1 >>> print (re. split (r ": |", "abc: dongguan shandong jinan") 2 ['abc', 'dongguan ', 'shandong', 'jinan'] 3 >> slice
========================================================== ======================================
Greedy and non-Greedy python
The quantifiers in python are greedy by default, that is, they will try to match as many characters as possible.
After ".", "*", "+", {m}, add ?, It turns greedy into non-greedy.
1 >>> print(re.match(r"aa(\d+)","aa2343ddd").group(1))2 23433 >>> print(re.match(r"aa(\d+?)","aa2343ddd").group(1))4 25 >>>