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:匹配并提取第一个符合规律或模式的内容,返回一个正则表达式对象。