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 matching is also divided into two types:
One, ordinary characters:
Most characters and letters will match themselves.
Import Reret = Re.findall ("admin", "Hello Administrator") #这是字符串的完整匹配print (ret)---------output-----------[' admin ']
Two, meta-characters:
The meta-character symbol includes: ". ^ $ * + ? {} [] () \ | "。
Let's take a closer look at the specific meanings and uses:
1, meta-character appended. ^ $ * + ? {} :
ret = Re.findall ("A...N", "Hello Administrator") # "." Represents any single character except \ n Print (ret) #-----> [' admin ']ret = Re.findall ("^h...o", "Hello Administrator") #匹配输入字符串的开始位置print ( RET) #-----> [' hello ']ret = Re.findall ("r...r$", "Hello Administrator") #匹配输入字符串的结束位置print (ret) #-----> [' rator '] ret = Re.findall ("abc*", "ABCCCCC") #匹配前面的子表达式零次或多次print (ret) #-----> [' abccccc ']ret = Re.findall ("abc+", "ABCCCCC") #匹配前面的子表达式一次或多次print (ret) #-----> [' abccccc ']ret = Re.findall ("ABC?", "ABCCCCC") #匹配前面的子表达式零次或一次print (ret) #----- > [' abc ']ret = Re.findall ("abc{1,3}", "ABCCCCC") #{} custom C occurrences of print (ret) #-----> [' ABCCC ']
Note: The preceding *,+,? And so on are greedy matches, that is, match as much as possible, then add the number to make it a non-greedy match (lazy match).
ret = Re.findall ("abc+?", "ABCCCCC") #非贪婪匹配print (ret) #-----> [' abc ']ret = Re.findall ("abc*?", "ABCCCC") #非贪婪匹配print ( RET) #-----> [' AB ']
2, meta-character appended character set []:
ret = Re.findall ("A[bc]d", "Abd") #匹配a和d之间有b或者cprint (ret) #-----> [' abd ']ret = Re.findall ("A[bc]d", "ACD") # Match A and d have B or Cprint (ret) #-----> [' ACD ']ret = Re.findall ("[A-Z]", "Hello") #匹配a-Z between all the letters print (RET) #-----> [' h ', ' E ', ' l ', ' l ', ' o ']ret = Re.findall ("[. *+?]", "a.b*c+d?e") #在 [] '. *+? ' No special meaning, just plain character print (ret) #-----> ['. ', ' * ', ' + ', '? '] #在字符集里有功能的符号:-(define a range) ^ (take reverse) \ (escape) ret = Re.findall ("[0-9]", "12ab34cd56ef") #取出所有的数字print (ret) #-----> [' 1 ', ' 2 ', ' 3 ' , ' 4 ', ' 5 ', ' 6 ']ret = Re.findall ("[^ab]", "12ab34cd56ef") #不取出abprint (ret) #-----> [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' C ', ' d ', ' 5 ', ' 6 ', ' e ', ' f ']ret = Re.findall ("[\d]", "12ab34cd56ef") #相当于 [0-9]print (ret) #-----> [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']
3, the escape character of the metacharacters \:
The backslash is followed by a meta-character to remove special functions, such as \., at this time. There is no special meaning, it is an ordinary character.
Backslash followed by ordinary characters to implement special functions, see below:
\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 a 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.
Python-based Re module (regular expression)