标签:
Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these ?
Modifier | Description |
---|---|
re.I | Performs case-insensitive matching. |
re.L | Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B). |
re.M | Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string). |
re.S | Makes a period (dot) match any character, including a newline. |
re.U | Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B. |
re.X | Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker. |
string2="ABCabc"
m=re.search("[a-z]+",string2)
m1=re.search("[a-z]+",string2,re.I)
print(m.group())
print(m1.group())
#结果:
abc
ABCabc
string3="yaobin \njack \nrain \nhy"
m=re.search("^y.+$",string3)
m1=re.search("^y.+$",string3,flags=re.M)
if m:
print("m: %s"%(m.group()))
if m1:
print("m1: %s"%(m1.group()))
#结果
m1: yaobin
string="abc \ndsf"
m=re.match("(^a.*$)",string)
m1=re.match("(^a.*$)",string,re.S)
if m:
print("m: %s"%(m.group()))
if m1:
print("m1: %s"%(m1.group()))
#结果:
m1: abc
dsf
标签:
原文地址:http://www.cnblogs.com/binhy0428/p/5221228.html