python是什么模块,python中什么是模块

## re模块

众多编程语言都为正则表达式的使用设置了标准库,Python也不例外。在Python中使用re模块来操作正则表达式的匹配。下面我们一起学习re模块的用法。

### 使用re还是compile对象

在re模块中可以直接调用一些正则表达式操作函数,而这些函数也可以使用compile

函数返回值对象来调用,只是被re调用时第一个参数为pattern,被regxobj调用时第一个不用加pattern,因为已经在调用compile函数时声明了pattern。所以功能是一样的,总结如下。

既能被re调用又能regxobj调用:

findall(string) 返回所有匹配到的字符串列表

finditer(string) 返回所有匹配到的对象生成迭代对象,

split(string) 将字符串以正则表达式分割

sub(replacestring,string,max)

subn(replacestring,string,max) 同sub只是返回替换的个数

match(string) 匹配一个匹配到的对象,这个对象必须以正则表达式匹配的字符串开头

search(string) 同match只是不用以这个正则表达式开头

下面分别介绍下这些函数的具体用法。

#### compile(pattern,flags=0)

使用compile()函数编译regex

- 对正则表达式模式pattern进行编译,flags是可选标志符,

并返回一个 regex 对象

regex对象有两个属性 groups和groupindex。groups是显示正则表达式有几个分组,groupindex是正则表达式中命名的分组形成的一个字典。

```

prog = repile(pattern)

result = prog.findall(string)

```

例子:

```

import re

p = repile('ab*')

```

* findall(pattern,string[,flags])

在字符串string中查找正则表达式模式pattern的所有(非重复)出现;返回一个匹配对象的列表

- findall()==总是返回一个列表==,如果findall()没有找到匹配的部分,就返回一个空列表,但如果匹配成功,列表将包含所有成功的匹配部分

```

re.findall('car', 'car')

re.findall('car', 'scary')

re.findall('car', 'carry the barcardi to the car') # ['car', 'car', 'car']

pattern = repile('car')

pattern.findall( 'carry the barcardi to the car', 13) # ['car', 'car']

pattern.findall( 'carry the barcardi to the car', 13, 16) # ['car']

s = 'this and that'

re.findall(r'thw+ and thw+', s, re.I) # ['this and that']

re.findall(r'(thw+)', s, re.I) # ['this', 'that'] *

re.findall(r'(thw+) and (thw+)', s, re.I) # [('this', 'that')] **

re.findall(r'(thw+) (and) (thw+)', s, re.I) # [('this', 'and', 'that')]

```

* finditer(pattern,string[, flags])

和 findall()相同,但返回的不是列表而是迭代器;对于每个匹配,该迭代器返回一个匹配对象

```

s = 'this and that'

re.finditer(r'(thw+) and (thw+)', s, re.I).__next__().groups() # ('this', 'that')

re.finditer(r'(thw+) and (thw+)', s, re.I).__next__().group() # 'this and that'

re.finditer(r'(thw+) and (thw+)', s, re.I).__next__().group(1) # 'this'

re.finditer(r'(thw+) and (thw+)', s, re.I).__next__().group(2) # 'that'

[g.groups() for g in re.finditer(r'(thw+) and (thw+)', s, re.I)] # [('this', 'that')]

```

* split(pattern,string, max=0)

根据正则表达式 pattern 中的分隔符把字符 string 分割为一个列表,返回成功匹配的列表,最多分割 max 次(默认是分割所有匹配的地方)。

- split(pattern, string, max=0) 根据正则表达式的模式分隔符,split函数将字符串分割为列表,然后返回成功匹配的列表,分隔最多操作max次 (默认分隔所有匹配成功的位置)

- 如果给定分隔符不是使用特殊符号来匹配多重模式的正则表达式,那么re.split()与str.split()的工作方式相同:

```

re.split(':', 'str1:str2:str3')

# ['str1', 'str2', 'str3']

```

```

import re

DATA = (

'Mountain View, CA 94040',

'Sunnyvale, CA',

'Los Altos, 94023',

'Cupertino 95014',

'Palo Alto CA',

)

for datum in DATA:

print (re.split(', |(?= (?:d{5}|[A-Z]{2}))', datum))

```

上述regex有一个简单的组件:使用split语句基于逗号分割字符串。更难的部分是最后的正则表达式和扩展符号。

* sub(pattern, repl, string, max=0)

把字符串 string 中所有匹配正则表达式 pattern 的地方替换成字符串 repl,如果 max 的值没有给出,则对所有匹配的地方进行替换(另外,请参考 subn(),它还会返回一个表示替换次数的数值)。

- 将某字符串中所有匹配正则表达式的部分进行某种形式的替换

- 用来替换的部分通常是一个字符串,也可能是一个函数,该函数返回一个用来替换的字符串

- subn()还返回一个表示替换的总数,替换后的字符串和表示替换总数的数字一起作为一个

拥有两个元素的元组返回

```

re.sub('X', 'Mr.Smith', 'attn:\\nDear X,\n')

# 'attn:Mr.Smit\\nDear Mr.Smith,\n'

re.subn('X', 'Mr.Smith', 'attn:\\nDear X,\n')

# ('attn:Mr.Smit\\nDear Mr.Smith,\n', 2)

print (re.sub('X', 'Mr.Smith', 'attn:\\nDear X,\n'))

#attn:Mr.Smith

#

#Dear Mr.Smith,

```

- 使用匹配对象的group()方法除了能够取出匹配分组标号外,还可以使用N, 其中N是在替换字符串中使用的分组编号。下面的代码仅仅只是将美式的日期表示法MM/DD/YY{,YY}格式转换为其他国家常用的格式DD/MM/YY{,YY}

```

re.sub(r'(d{1,2}) / (d{1,2}) / (d{2} | d{4})', r'//', '2/20/91')

re.sub(r'(d{1,2}) / (d{1,2}) / (d{2} | d{4})', r'//', '2/20/1991')

```

* match(pattern,string, flags=0)

尝试用正则表达式模式pattern匹配字符串string,flags是可选标志符,如果匹配成功,则返回一个Match匹配对象;否则返回 None。这里注意,match正则表达式只能匹配字符串的开始。如果不是开始地方则无法匹配。并且如果匹配到的字符串出现了多次也只能匹配一次。

- 试图从字符串的起始部分对模式进行匹配

- 匹配成功就返回一个匹配对象;匹配失败,返回None

- 匹配对象的group()方法能用于显示那个成功的匹配

```

result = re.match(pattern, string)

```

equivalent to:

```

prog = repile(pattern)

result = prog.match(string)

```

例子:

```

import re

m = re.match('foo', 'food on the table')

if m is not None:

m.group() # 返回匹配对象foo

```

==注意:在下面的例子中,省略if语句,如果匹配失败,会抛出AttributeError异常==

```

re.match('foo', 'food on the table').group()

```

```

pattern = repile("o")

pattern.match('doodle')

pattern.match('doodle',1)

#

pattern.match('doodle',2)

#

```

* search(pattern,string, flags=0)

在字符串 string 中查找正则表达式模式 pattern 的第一次出现,flags 是可选标志符,如果匹配成功,则返回一个Match匹配对象;否则返回 None。这看起来和match相同,但唯一的不同是search可以匹配任意位置。

```

import re

m = re.match('foo', 'seafood') # 匹配失败

if m is not None:

m.group()

```

```

import re

m = re.search('foo', 'food on the table')

if m is not None:

m.group()

```

```

pattern = repile("d")

pattern.search("dog") # Match at index 0

#

pattern.search("dog", 1)

pattern.search('doodle', 1,4)

bt = 'bat | bet | bit'

m = re.match(bt, 'bat man')

# m = re.match(bt, 'He bit me!') # not match

# m = re.search(bt, 'He bit me!') # match

#m = re.match(bt, 'batman') # not match

# m = re.search(bt, 'batman') # not match

if m is not None:

m.group()

# m.group(0) #num 0 is the number of subgroups in the matching object

```

#### 关于flags标志位

在 re 模块中标志可以使用两个名字,一个是全名如 IGNORECASE,一个是缩写,一字母形式如 I。多个标志可以通过按位 OR-ing 它们来指定。如 re.I | re.M

被设置成 I 和 M 标志:

标志 含义

DOTALL, S 使 . 匹配包括换行在内的所有字符

IGNORECASE, I 使匹配对大小写不敏感

MULTILINE, M 多行匹配,影响 ^ 和 $

VERBOSE, X 能够使用 REs 的 verbose 状态,使之被组织得更清晰易懂

I

IGNORECASE

使匹配对大小写不敏感;字符类和字符串匹配字母时忽略大小写。举个例子,[A-Z]也可以匹配小写字母,Spam 可以匹配 "Spam", "spam", 或 "spAM"。这

个小写字母并不考虑当前位置。

M

MULTILINE

使用 "^" 只匹配字符串的开始,而 $ 则只匹配字符串的结尾和直接在换行前(如果有的话)的字符串结尾。当本标志指定后, "^" 匹配字符串的开始和字符串

中每行的开始。同样的, $ 元字符匹配字符串结尾和字符串中每行的结尾(直接在每个换行之前)。

S

DOTALL

使 "." 特殊字符完全匹配任何字符,包括换行;没有这个标志, "." 匹配除了换行外的任何字符。

X

VERBOSE

该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。当该标志被指定时,在 RE 字符串中的空白符被忽略,除非该空白符在字符类中或在反斜杠之后;这可以让你更清晰地组织和缩进 RE。它也可以允许你将注释写入 RE,这些注释会被引擎忽略;注释用 "#"号 来标识,不过该符号不能在字符串或反斜

杠之后。

### Match对象的使用

#### 属性

pos:目标字符串的开始位置

endpos:目标字符串的结束位置

lastgroup:最后一个组的组名

lastindex: 最后一个组是第几个组(从1开始计数)

```

regex = repile('(ab)cd(?Pef)')

match_obj=regex.search('abcdefg')

b.lastgroup

'name'

b.lastindex

2

```

#### 方法

*start()

返回匹配到的字符串的起始位置

* end()

返回匹配到的字符串的起始位置

*span()

返回匹配到的字符串的起始位置和终止位置

* group(num=0)

返回全部匹配对象(或指定编号是 num 的子组)

```

m = re.match('(www)-(ddd)', 'abc-123')

print (m.group()) #完整匹配 ‘abc-123'

print (m.group(1)) #子组1 'abc'

print (m.group(2)) #子组2 '123'

print (m.groups()) #全部子组 ('abc', '123')

```

* groups()

返回一个包含全部匹配的子组的元组(如果没有成功匹配,就返回一个空元组)

```

m = re.match('ab','ab')

m.group()

m.groups()

m = re.match('(ab)', 'ab')

m.group()

m.group(1)

m.groups()

m = re.match('(a)(b)', 'ab')

m.group()

m.group(1)

m.group(2)

m.groups()

m = re.match('(a)(b)', 'ab')

m.group()

m.group(1)

m.group(2)

m.groups()

```

```

patt1 = '^(w){3}'

patt2 = '^(www)'

patt3 = '^(w{3})'

m = re.match(patt2, 'Wed 123')

```

* groupdict()

返回一个包含全部匹配的子组的字典(要求子组有名称,名称为键,匹配到的内容为值)

```

regex = repile('(ab)cd(?Pef)')

match_obj=regex.search('abcdefg')

match_obj.groupdict()

{'name': 'ef'}

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值