RE模块的操作:Regular Expression (描述某种规则) Match object (匹配对象) result = re.match (正则表达式,要匹配的字符串) match方法返回匹配对象,否则返回None.(注意不是空字符串“”) 匹配对象Macth Object具有group方法,用来返回匹配对象。
匹配单个字符
字符 功能
- . 匹配任意 1 个字符(除了\n)
- [ ] 匹配[ ]中列举的字符
- \d 匹配数字,即 0-9
- \D 匹配非数字,即不是数字
- \s 匹配空白,即空格,tab 键
- \S 匹配非空白
- \w 匹配单词字符,即 a-z、A-Z、0-9、_
- \W 匹配非单词字符
匹配多个字符
字符 功能
* 匹配前一个字符出现 0 次或者无限次,即可有可无
+ 匹配前一个字符出现 1 次或者无限次,即至少有 1 次
? 匹配前一个字符出现 1 次或者 0 次,即要么有 1 次,要么没有
{m} 匹配前一个字符出现 m 次
{m, n} 匹配前一个字符出现从 m 到 n 次
In [8]: re.match("^a[^b]","a ").group()
Out[8]: 'a '
#[^b]表示除了b以外都要
In [9]: re.match('\d?','abc').group()
Out[9]: ''
#'abc'可以看成' abc'
In [10]: re.match('\d?','abc')
Out[10]: <_sre.SRE_Match at 0x7efc73946e68>
In [11]: re.match('\d+','abc')
In [12]: re.match('\d+','1bc')
Out[12]: <_sre.SRE_Match at 0x7efc73260f38>
In [13]: re.match('\d*','abc')
Out[13]: <_sre.SRE_Match at 0x7efc73260ed0>
In [14]: re.match('\d*','abc').group()
Out[14]: ''
In [15]: re.match('\d*','1bc').group()
Out[15]: '1'
In [16]: re.match('\d+','1bc').group()
Out[16]: '1'
In [17]: re.match('\d?','1bc').group()
Out[17]: '1'
关于字符转译的问题
In [2]: s = '\nabc'
In [3]: print(s)
abc
In [4]: import re
In [5]: re.match('\\nabc',s)
Out[5]: <_sre.SRE_Match object; span=(0, 4), match='\nabc'>
In [6]: s = '\\abc'
In [7]: re.match('\\\\abc',s)
Out[7]: <_sre.SRE_Match object; span=(0, 4), match='\\abc'>
re.match()里加r(原始字符串忽略转译)==>>re.match(r” “,” “)
re.match(r”//abc”,”//abc”)
关于边界问题
In [8]: re.match(r"[1-9]\d?|0|100","200")
Out[8]: <_sre.SRE_Match object; span=(0, 2), match='20'>
In [9]: re.match(r"[1-9]\d?$|0$|100$","200")
In [10]: re.match(r"[1-9]\d?$|0$|100$","100")
Out[10]: <_sre.SRE_Match object; span=(0, 3), match='100'>
In [11]: re.match(r"[1-9]\d?$|0$|100$","08")
In [12]: re.match(r"[1-9]\d?$|0$|100$","084")
In [4]: re.match(r"[1-9]?\d?$|100$","0")
Out[4]: <_sre.SRE_Match object; span=(0, 1), match='0'>
In [9]: re.match(r"^\w+ve","hover")
Out[9]: <_sre.SRE_Match object; span=(0, 4), match='hove'>
In [10]: re.match(r"^\w+ve\b","hover")
#\b匹配单词边界 \B表示匹配非单词边界
#简单的来说\B在单词中划分边界
In [13]: re.match(r"^.+ve\B","ho ve r")匹配不到
In [14]: re.match(r"^.+ve\B","ho ver")
Out[14]: <_sre.SRE_Match object; span=(0, 5), match='ho ve'>
In [11]: re.match(r"^\w+\s\bve\b","ho ve r")
Out[11]: <_sre.SRE_Match object; span=(0, 5), match='ho ve'>
#\s此处表示空格
关于分组,group()与groups()的用法
In [23]: re.match(r"<h1>(.*)</h1>","<h1>匹配分组</h1>").groups(
...: )[0]
Out[23]: '匹配分组'
In [24]: re.match(r"<h1>(.*)</h1>","<h1>匹配分组</h1>").group()
...:
Out[24]: '<h1>匹配分组</h1>'
In [25]: re.match(r"<h1>(.*)</h1>","<h1>匹配分组</h1>").group(0
...: )
Out[25]: '<h1>匹配分组</h1>'
In [26]: re.match(r"(<h1>)(.*)(</h1>)","<h1>匹配分组</h1>").gro
...: ups(0)
Out[26]: ('<h1>', '匹配分组', '</h1>')
In [28]: re.match(r"(<h1>)(.*)(</h1>)","<h1>匹配分组</h1>").gro
...: ups(0)[1]
Out[28]: '匹配分组'
关于search,sub,findall的用法
In [43]: re.sub(r"\d+","50","python=1000,php=0")
Out[43]: 'python=50,php=50'
In [44]: re.sub(r"php","python","itcast python cpp php python p
...: hp")
Out[44]: 'itcast python cpp python python python'
In [47]: re.sub(r"\d+","50","python=1000,php=0")
Out[47]: 'python=50,php=50'
In [48]: re.search(r"\d+","python=1000,php=0")
Out[48]: <_sre.SRE_Match object; span=(7, 11), match='1000'>
In [49]: re.findall(r"\d+","python=1000,php=0")
Out[49]: ['1000', '0']
In [45]: def replace(result):
...: print(result.group())
...: r = int(result.group())+50
...: return str(r)
...: re.sub(r"\d+",replace,"python=1000,php=0")
...:
1000
0
Out[45]: 'python=1050,php=50'
#可以看出sub是直接出结果,不用加.group()
re.sub(r"匹配规则","替换数据","被查询的内容")
#search需要加.group()得到结果,只查询到符合的第一个结果
re.search(r"需要查询的数据","被查询内容").group()
#findall直接出结果,以列表的形式返回所有符合要求的查询结果,不用加.group()
re.search(r"需要查询的数据","被查询内容")
关于split分割字符串,并返回一个列表
In [55]: re.split(r":| ","info:xiaozhang 33 shandong")
Out[55]: ['info', 'xiaozhang', '33', 'shandong']
In [56]: re.split(r":| ","info:xiaozhang 33 shandong").group()
---------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-56-3843c92c7d71> in <module>()
----> 1 re.split(r":| ","info:xiaozhang 33 shandong").group()
AttributeError: 'list' object has no attribute 'group'
# re.split(r"以字符拆分,选定(:或 )","匹配内容")
贪婪模式与非贪婪
In [58]: re.match(r".+(\d+-\d+-\d+-\d+)",s)
Out[58]: <_sre.SRE_Match object; span=(0, 31), match='This is a number 234-235-22-423'>
In [59]: re.match(r".+(\d+-\d+-\d+-\d+)",s).group(1)
Out[59]: '4-235-22-423'
In [60]: re.match(r"(.+)(\d+-\d+-\d+-\d+)",s).group(1)
Out[60]: 'This is a number 23'
In [61]: re.match(r"(.+)(\d+-\d+-\d+-\d+)",s).group()
Out[61]: 'This is a number 234-235-22-423'
In [62]: re.match(r"(.+)(\d+-\d+-\d+-\d+)",s).groups()
Out[62]: ('This is a number 23', '4-235-22-423')
In [63]: re.match(r"(.+?)(\d+-\d+-\d+-\d+)",s).groups()
Out[63]: ('This is a number ', '234-235-22-423')
# .+/.*/.?贪婪模式下尽可能的多取
# 后面加?关闭贪婪模式,变成非贪婪