热线电话:13121318867

登录
2020-11-05 阅读量: 739
如何理解compile函数

如何理解compile函数

函数compile()

正则表达式被编译成模式对象,模式对象具有各种操作的方法,例如搜索模式匹配或执行字符串替换。

# Module Regular Expression is imported using __import__().

import re


# compile() creates regular expression character class [a-e],

# which is equivalent to [abcde].

# class [abcde] will match with string with 'a', 'b', 'c', 'd', 'e'.

p = re.compile('[a-e]')


# findall() searches for the Regular Expression and return a list upon finding

print(p.findall("Aye, said Mr. Gibenson Stark"))


输出:

['e','a','d','b','e','a']

理解输出:

第一次出现在“Aye”中是'e'而不是'A',因为它是区分大小写的。

接下来的事件是“说”中的'a',然后是'说'中的'd',然后是'Gibenson'中的'b'和'e',最后'a'与“Stark”相匹配。


35.2538
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子