day⑤:re深入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day⑤:re深入相关的知识,希望对你有一定的参考价值。

Regular Expression Modifiers: Option Flags

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 ?

ModifierDescription
re.IPerforms case-insensitive matching.
re.LInterprets 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.MMakes $ 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.SMakes a period (dot) match any character, including a newline.
re.UInterprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
re.XPermits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.

①.re.I #忽略大小写
  1. string2="ABCabc"
  2. m=re.search("[a-z]+",string2)
  3. m1=re.search("[a-z]+",string2,re.I)
  4. print(m.group())
  5. print(m1.group())
  6. #结果:
  7. abc
  8. ABCabc


②re.M #多行,"^"匹配每行的开头,"$"匹配每行的末尾,只影响^和$
  1. string3="yaobin \njack \nrain \nhy"
  2. m=re.search("^y.+$",string3)
  3. m1=re.search("^y.+$",string3,flags=re.M)
  4. if m:
  5. print("m: %s"%(m.group()))
  6. if m1:
  7. print("m1: %s"%(m1.group()))
  8. #结果
  9. m1: yaobin


③re.S #使"."匹配包含换行符在内的任意字符
  1. string="abc \ndsf"
  2. m=re.match("(^a.*$)",string)
  3. m1=re.match("(^a.*$)",string,re.S)
  4. if m:
  5. print("m: %s"%(m.group()))
  6. if m1:
  7. print("m1: %s"%(m1.group()))
  8. #结果:
  9. m1: abc
  10. dsf






以上是关于day⑤:re深入的主要内容,如果未能解决你的问题,请参考以下文章

day⑤:冒泡排序

HQ-day4 C#语句实例⑤判断一个日期是否正确

Day657.思考题解答⑤ -Java业务开发常见错误

Day495&496.尚硅谷之高频重点面试题⑤ -面经

apriori片段代码

短视频运营短视频剪辑 ⑤ ( 视频素材使用 | 设置插入后的视频素材属性 | 设置画面 | 设置音频 | 设置变速 | 设置动画 | 设置调节 )