加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python学习笔记九:正则表达式

发布时间:2020-12-17 00:04:00 所属栏目:Python 来源:网络整理
导读:本文不涉及正则表达式本身的内容,只记一下python中正则的用法及常用方法。 一、re模块 python中用re模块进行正则处理 >>> >>>s = r >>>re.findall(s, ] 或先编译(会更快): >>> >>> r1 = re.compile(r >>> re.findall(r1, span style="color: #008000;"#

本文不涉及正则表达式本身的内容,只记一下python中正则的用法及常用方法。

一、re模块

python中用re模块进行正则处理

>>>>>>s = r >>>re.findall(s,]

或先编译(会更快):

>>> >>> r1 = re.compile(r>>> re.findall(r1,<span style="color: #008000;">#<span style="color: #008000;">或用下面的方式

r1.findall(<span style="color: #800000;">'<span style="color: #800000;">ababcabd<span style="color: #800000;">'<span style="color: #000000;">)<span style="color: #000000;">
[<span style="color: #800000;">'<span style="color: #800000;">abc<span style="color: #800000;">']

1、compile:对正则进行编译,使运行速度加快,用法如上

2、match:决定RE是否在字符串刚开始的位置,返回的是Match object对象,没匹配到返回None

3、search:扫描字符串,找到这个 RE 匹配的位置,返回的是Match object对象,没匹配到返回None

4、findall:找到RE匹配的所有子串,作为列表返回

5、finditer:找到RE匹配的所有子串,作为迭代器返回,迭代器中是 Match object对象

6、sub(regex,replace【可以是函数】,content,count=0【可选,替换的数量,默认为全部】,flags=0【可选,正则参数,如re.I】):数据替换

详细参考:

7、subn(regex,replace,count=0,flags=0):跟sub相同,只是返回值中包含替换次数

8、split(string [,maxsplit = 0]):把字符串按正则分隔

三、Match object 中常用方法

1、group():返回被RE匹配的字符串

2、start():返回匹配的开始的位置

3、end():返回匹配结束的位置

4、span():返回一个元组包含匹配(开始和结束)的位置

示例:

= re.match(r.*),

<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.string:<span style="color: #800000;">"<span style="color: #000000;">,m.string
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.re:<span style="color: #800000;">"<span style="color: #000000;">,m.re
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.pos:<span style="color: #800000;">"<span style="color: #000000;">,m.pos
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.endpos:<span style="color: #800000;">"<span style="color: #000000;">,m.endpos
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.lastindex:<span style="color: #800000;">"<span style="color: #000000;">,m.lastindex
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.lastgroup:<span style="color: #800000;">"<span style="color: #000000;">,m.lastgroup

<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.group(1,2):<span style="color: #800000;">",m.group(1,2<span style="color: #000000;">)
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.groups():<span style="color: #800000;">"<span style="color: #000000;">,m.groups()
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.groupdict():<span style="color: #800000;">"<span style="color: #000000;">,m.groupdict()
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.start(2):<span style="color: #800000;">",m.start(2<span style="color: #000000;">)
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.end(2):<span style="color: #800000;">",m.end(2<span style="color: #000000;">)
<span style="color: #0000ff;">print <span style="color: #800000;">"<span style="color: #800000;">m.span(2):<span style="color: #800000;">",m.span(2<span style="color: #000000;">)
<span style="color: #0000ff;">print r<span style="color: #800000;">"<span style="color: #800000;">m.expand(r'2 13'):<span style="color: #800000;">",m.expand(r<span style="color: #800000;">'<span style="color: #800000;">2 13<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;">## output ###<span style="color: #008000;">

<span style="color: #008000;"> m.string: hello world!<span style="color: #008000;">

<span style="color: #008000;"> m.re: <_sre.SRE_Pattern object at 0x016E1A38><span style="color: #008000;">

<span style="color: #008000;"> m.pos: 0<span style="color: #008000;">

<span style="color: #008000;"> m.endpos: 12<span style="color: #008000;">

<span style="color: #008000;"> m.lastindex: 3<span style="color: #008000;">

<span style="color: #008000;"> m.lastgroup: sign<span style="color: #008000;">

<span style="color: #008000;"> m.group(1,2): ('hello','world')<span style="color: #008000;">

<span style="color: #008000;"> m.groups(): ('hello','world','!')<span style="color: #008000;">

<span style="color: #008000;"> m.groupdict(): {'sign': '!'}<span style="color: #008000;">

<span style="color: #008000;"> m.start(2): 6<span style="color: #008000;">

<span style="color: #008000;"> m.end(2): 11<span style="color: #008000;">

<span style="color: #008000;"> m.span(2): (6,11)<span style="color: #008000;">

<span style="color: #008000;"> m.expand(r'2 13'): world hello!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读