鱼C论坛

 找回密码
 立即注册

正则表达式

已有 215 次阅读2018-4-13 12:15

compile函数:
通常我们会将字符串先编译成pattern实例,然后在进行匹配的操作,而创建pattern实例就是要用到compile函数。
Help on function compile in module re:

compile(pattern, flags=0)
    Compile a regular expression pattern, returning a pattern object.(编译一个正则表达式模式,返回一个模式对象)

Help on function findall in module re:  findall:匹配所有符合规律(模式)的内容。返回包含结果的列表。

findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.
    返回字符串中所有非重叠匹配的列表。
    If one or more capturing groups are present in the pattern, returna list of groups; 
    如果模式中存在一个或多个捕获组,则返回组的列表
    this will be a list of tuples if the pattern has more than one group.
    如果模式有多个组,这将是一个元组列表
    Empty matches are included in the result.
    结果中包含空的匹配项。

下面是一些正则表达式常用符号与方法
(.):匹配任意字符,换行符\n除外
 (*): 匹配前一字符0次或无限次
(?):匹配前一字符0次或1次
(.*):贪心算法
(.*?):非贪心算法
():括号内的数据作为结果返回
贪心算法:
re.sub:替换符合规律的内容,返回替换后的值
re.search:匹配并提取第一个符合规律或模式的内容,返回一个正则表达式对象。

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-6 23:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部