Analysis of Python Regular Expression re. match and re. search and re. sub

  • 2021-07-24 11:20:53
  • OfStack

From Python Regular Expression

re.match(pattern, string, flags=0)

Attempt to match 1 pattern from the beginning of string; re. match () returns none if the starting position is not matched successfully.

If the match is successful, re. match () returns 1 matching object, otherwise None is returned.

pattern-matched regular expressions

string-String to match

flags-Flag bit that controls how regular expressions are matched, such as case-sensitive, multi-line matching, and so on.

e.g.


#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import re
print(re.match('www', 'www.runoob.com').span()) #  Match in the starting position 
print(re.match('com', 'www.runoob.com'))  #  Do not match at the starting position 


output:
(0, 3)
None

re.search(pattern, string, flags=0)

Scan the entire string and return the first successful match.

If the match is successful, re. search () returns 1 matching object, otherwise None is returned.

e.g.


#!/usr/bin/python
# -*- coding: UTF-8 -*- 

import re
print(re.search('www', 'www.runoob.com').span()) #  Match in the starting position 
print(re.search('com', 'www.runoob.com').span()) #  Do not match at the starting position 


output:
(0, 3)
(11, 14)

re. match () only matches the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and the function returns None;

re. search () matches the entire string until one match is found.

re.sub(pattern, repl, string, count=0, flags=0)

Replaces the match in the string.

pattern-Pattern Strings in Regular

repl-Replaced string, can also be 1 function

string-The original string to be found and replaced

count-Maximum number of replacements after pattern matching. The default is 0 to replace all matches

e.g.


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re

phone = "2004-959-559

#  Delete the  Python Notes  
num = re.sub(r'#.*$', "", phone)
print " The telephone number is : ", num

#  Delete non-digits (-) String of  
num = re.sub(r'\D', "", phone)
print " The telephone number is  : ", num


output:
 The telephone number is : 2004-959-559 
 The telephone number is  : 2004959559

Regular expression pattern

模式 描述
^ 匹配字符串的开头
$ 匹配字符串的末尾
. 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符
[…] 用来表示1组字符,单独列出:[amk] 匹配 ‘a','m'或'k'
[^…] 不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符
re* 匹配0个或多个的表达式
re+ 匹配1个或多个的表达式
re? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
re{ n}
re{ n,} 精确匹配n个前面表达式
re{ n, m} 匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式
aIb 匹配a或b
(re) G匹配括号内的表达式,也表示1个组
(?imx) 正则表达式包含3种可选标志:i, m, 或 x, 只影响括号中的区域
(?-imx) 正则表达式关闭 i, m, 或 x 可选标志, 只影响括号中的区域
(?: re) 类似 (…), 但是不表示1个组
(?imx: re) 在括号中使用i, m, 或 x 可选标志
(?-imx: re) 在括号中不使用i, m, 或 x 可选标志
(?#…) 注释
(?= re) 前向肯定界定符. 如果所含正则表达式,以 … 表示,在当前位置成功匹配时成功,否则失败. 但1旦所含表达式已经尝试,匹配引擎根本没有提高;模式的剩余部分还要尝试界定符的右边.
(?! re) 前向否定界定符. 与肯定界定符相反;当所含表达式不能在字符串当前位置匹配时成功
(?> re) 匹配的独立模式,省去回溯
\w 匹配字母数字及下划线
\W 匹配非字母数字及下划线
\s 匹配任意空白字符,等价于 [\t\n\r\f]
\S 匹配任意非空字符
\d 匹配任意数字,等价于 [0-9].
\D 匹配任意非数字
\A 匹配字符串开始
\Z 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串
\z 匹配字符串结束
\G 匹配最后匹配完成的位置
\b 匹配1个单词边界,也就是指单词和空格间的位置.例如, ‘er\b' 可以匹配”never” 中的 ‘er',但不能匹配 “verb” 中的 ‘er'.
\B 匹配非单词边界. ‘er\B' 能匹配 “verb” 中的 ‘er',但不能匹配 “never” 中的 ‘er'.
\n, \t, 等. 匹配1个换行符。匹配1个制表符, 等
\1…\9 匹配第n个分组的内容.
\10 匹配第n个分组的内容,如果它经匹配. 否则指的是8进制字符码的表达式.

Regular expression instance

实例 描述
python 匹配 “python”
[Pp]ython 匹配 “Python” 或 “python”
rub[ye] 匹配 “ruby” 或 “rube”
[aeiou] 匹配中括号内的任意1个字母
[0-9] 匹配任何数字,类似于 [0123456789]
[a-z] 匹配任何小写字母
[A-Z] 匹配任何大写字母
[a-zA-Z0-9] 匹配任何字母及数字
[^aeiou] 除了aeiou字母以外的所有字符
[^0-9] 匹配除了数字外的字符
. 匹配除 “\n” 之外的任何单个字符。要匹配包括 ‘\n' 在内的任何字符,请使用象 ‘[.\n]' 的模式.
\d 匹配1个数字字符, 等价于 [0-9].
\D 匹配1个非数字字符, 等价于 [^0-9].
\s 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于 [ \f\n\r\t\v].
\S 匹配任何非空白字符, 等价于 [^ \f\n\r\t\v].
\w 匹配包括下划线的任何单词字符, 等价于'[A-Za-z0-9_]'.
\W 匹配任何非单词字符, 等价于 ‘[^A-Za-z0-9_]'.

Related articles: