8. Re module: Regular expression
In essence, a regular expression (or RE) is a small, highly specialized programming language (in Python) that is embedded in Python and 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 match (normal character, metacharacters):
1. Ordinary characters: Most characters and letters will match themselves
>>> Re.findall (' Alvin ', ' Yuanalesxalexwupeiqi ')
[' Alvin ']
2. Meta-characters:. ^ $ * + ? { } [ ] | ( ) \
. Match any character except newline characters ^ match string at the beginning of the $ match string at the end of the # repeating metacharacters four kinds: * match 0 or more expressions, greedy way, after add to the number to make it lazy match + match 1 or more expressions, greedy way, The back plus number makes it an inert match? Matches 0 or 1 fragments defined by the preceding regular expression, the non-greedy way {m,n} matches the specified number of times (m-n) of the expression, greedy way, followed by the number to make it an inert match a| B match A or b
Metacharacters's character set []:
metacharacters Character Set []:ret=re.findall (' a[bc]d ', ' ACD ') print (ret) #[' ACD '] ret=re.findall (' [A-z] ', ' ACD ') print (ret) #[' A ', ' C ', ' d ' ] Ret=re.findall (' [. *+] ', ' a.cd+ ') print (ret) #['. ', ' + '] #在字符集里有功能的符号:-^ ret=re.findall (' [1-9] ', ' 45dha3 ') # Match character 1-9print (ret) #[' 4 ', ' 5 ', ' 3 '] ret=re.findall (' [^ab] ', ' 45bdha3 ') #^ inverse, match A, b characters other than print (ret) #[' 4 ', ' 5 ', ' d ', ' H ', ' 3 '] ret=re.findall (' [\d] ', ' 45bdha3 ') #\d match character print (ret) #[' 4 ', ' 5 ', ' 3 ']
Escape character of metacharacters: \
1, backslash followed by meta-character removal of special functions, such as \.
2, back slash behind with ordinary characters to achieve special functions, such as \d
\d matches any decimal number; it is equivalent to class [0-9]. \d matches any non-numeric character; it is equivalent to class [^0-9]. \s matches any whitespace character; it is equivalent to class [\t\n\r\f\v]. \s matches any non-whitespace character; it is equivalent to class [^ \t\n\r\f\v]. \w matches any alphanumeric character; it is equivalent to class [a-za-z0-9_]. \w matches any non-alphanumeric character; it is equivalent to a class [^a-za-z0-9_]\b matches a special character boundary, such as a space, &,#, etc.
Metacharacters Group: (), available? P<name> setting group names with (uppercase P)
m = Re.findall (R ' (AD) + ', ' Add ') print (m) ret=re.search (' (? P<id>\d{2})/(? P<name>\w{3}) ', ' 23/com ') print (Ret.group ()) #23/comprint (ret.group (' id '))#23
Meta-character appended: |
Ret=re.search (' (AB) |\d ', ' RABHDG8SD ') print (Ret.group ()) #ab
Common methods under the RE module
Import Re#1re.findall (' A ', ' Alvin Yuan ') #返回所有满足匹配条件的结果, put in the list #2re.search (' A ', ' Alvin Yuan '). Group () # The function finds a pattern match within the string, only to find the first match and then returns an object that contains the matching information, which can get a matching string by calling the group () method, or None if the string does not match. #3re. Match (' A ', ' ABC '). Group () #同search, but only at the beginning of the string to match #4ret =re.split (' [ab] ', ' ABCD ') #先按 ' A ' division get ' and ' BCD ', In the pair ' and ' BCD ' respectively press ' B ' split print (ret) #[', ' ', ' CD '] #5ret =re.sub (' \d ', ' abc ', ' Alvin5yuan6 ', 1) print (ret) #alvinabcyuan6ret = Re.subn (' \d ', ' abc ', ' Alvin5yuan6 ') print (ret) # (' Alvinabcyuanabc ', 2) #6obj =re.compile (' \d{3} ') Ret=obj.search (' Abc123eeee ') print (Ret.group ()) #123
Add:
Import re Ret=re.findall (' www. ( baidu|oldboy). com ', ' www.oldboy.com ') print (ret) #[' Oldboy ') This is because FindAll will first return the contents of the matching result set, and if you want to match the result, cancel the permission ret= Re.findall (' www. ( ?:baidu|oldboy). com ', ' www.oldboy.com ') print (ret) #[' www.oldboy.com ']
Python Foundation (_python) module re module (regular expression)