正则匹配的六种模式

re.I    IGNORECASE, 忽略大小写的匹配模式

 
  1. In [59]: s = 'hello World!'

  2.  
  3. In [60]: regex = re.compile("hello world!", re.I)

  4.  
  5. In [61]: regex.match(s).group()

  6. Out[61]: 'hello World!'

re.M    MULTILINE,多行模式, 改变 ^ 和 $ 的行为

 
  1. In [63]: s

  2. Out[63]: 'first line\nsecond line\nthird line'

  3.  
  4. In [64]: pattern=re.compile(r'^\w+')

  5.  
  6. In [65]: re.findall(pattern,s)

  7. Out[65]: ['first']

  8.  
  9. In [67]: pattern=re.compile(r'^\w+',re.M)

  10.  
  11. In [68]: re.findall(pattern,s)

  12. Out[68]: ['first', 'second', 'third']

re.S   DOTALL,此模式下 '.' 的匹配不受限制,可匹配任何字符,包括换行符,也就是默认是不能匹配换行符

 
  1. In [62]: s = '''first line

  2. ...: second line

  3. ...: third line'''

  4.  
  5. In [71]: regex=re.compile('.+',re.S)

  6.  
  7. In [73]: regex.findall(s)

  8. Out[73]: ['first line\nsecond line\nthird line']

  9.  
  10. In [74]: regex=re.compile('.+')

  11.  
  12. In [75]: regex.findall(s)

  13. Out[75]: ['first line', 'second line', 'third line']

re.X    VERBOSE,冗余模式, 此模式忽略正则表达式中的空白和#号的注释

 
  1. email_regex = re.compile("[\w+\.]+@[a-zA-Z\d]+\.(com|cn)")

  2.  
  3. email_regex = re.compile("""[\w+\.]+ # 匹配@符前的部分

  4. @ # @符

  5. [a-zA-Z\d]+ # 邮箱类别

  6. \.(com|cn) # 邮箱后缀 """, re.X)

猜你喜欢

转载自blog.csdn.net/yizhuanlu9607/article/details/81186828