python学习之re 9 (?aiLmsux) local集合

时间:2022-12-13 00:10:36

(?aiLmsux)(One or more letters from the set  'a''i''L''m''s''u''x'.) The group matches the empty string; the letters set the corresponding flags:  re.A (ASCII-only matching),  re.I (ignore case),  re.L (locale dependent), re.M (multi-line),  re.S (dot matches all),  re.U (Unicode matching), and  re.X (verbose), for the entire regular expression. (The flags are described in  Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a  flag argument to the  re.compile() function. Flags should be used first in the expression string.

翻译:(一个或者多个字符来自于集合 'a''i''L''m''s''u''x')这种组匹配空字符串;不同的字符对应不同的集合。如re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent),re.M (multi-line), re.S (dot matches all), re.U (Unicode matching), and re.X (verbose),对于所有的正则表达式(所有的flag在模块中有描述,在后面的博客也将会介绍到)如果你想将这些集合作为你RE表达式的一部分(混合匹配),这种语法是很有用的,而不是传入flag参数到complie中(传入了参数也就固定了格式),这些标识应该放在RE表达串的头部

案例一

import re
string1 = """hello,
world."""
print(len(string1),re.match("(?s).*",string1))
print(len(string1),re.match("(?m).*?,$\n.*?.$",string1))

输出

13 <re.Match object; span=(0, 13), match='hello,\nworld.'>
13 <re.Match object; span=(0, 13), match='hello,\nworld.'>

在这里s就等价于re.DOTALL re.S。然后m标识多行,m主要用于多行匹配标识符有  ^ $.

其他的标识符将不做演示。

import re
string1 = """hello,
world.
hello,
python."""
print(len(string1),re.match("(?sm).*?\..*?,$\n.*?.$",string1))

输出

28 <re.Match object; span=(0, 28), match='hello,\nworld.\nhello,\npython.'>
然后通过本案例我们可以得出结论就是标识符是可以多个嵌套的。