re模块(正则表达式)

1. 什么是正则

正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

2. 常用匹配模式(元字符)

 

# =================================匹配模式=================================
# 一对一的匹配
# 'hello'.replace(old,new)
# 'hello'.find('pattern')

# 正则匹配
import re

# \w与\W
print(re.findall('\w', 'hello egon 123'))  # ['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
print(re.findall('\W', 'hello egon 123'))  # [' ', ' ']

# \s和\S
print(re.findall('\s', 'hello  egon  123'))  # [' ', ' ', ' ', ' ']
print(re.findall('\S', 'hello  egon  123'))  # ['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']

# \n \t都是空,都可以被\s匹配
print(re.findall('\s', 'hello \n egon \t 123'))  # [' ', '\n', ' ', ' ', '\t', ' ']

# \n与\t
print(re.findall(r'\n', 'hello egon \n123'))  # ['\n']
print(re.findall(r'\t', 'hello egon\t123'))  # ['\t']

# \d与\D
print(re.findall('\d', 'hello egon 123'))  # ['1', '2', '3']
print(re.findall('\D', 'hello egon 123'))  # ['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']

# \A与\Z
print(re.findall('\Ahe', 'hello egon 123'))  # ['he'],\A==>^
print(re.findall('123\Z', 'hello egon 123'))  # ['123'],\Z==>$
'''
^:指定的匹配必须出现在字符串的开头或行的开头
\A:指定的匹配必须出现在字符串的开头(忽略 Multiline 选项)
$:指定的匹配必须出现在以下位置:字符串结尾、字符串结尾的 \n 之前或行的结尾
\Z:指定匹配必须出现在字符串的结尾或字符串结尾的 \n 之前(忽略 Multiline 选项)
'''

# ^与$
print(re.findall('^h', 'hello egon 123'))  # ['h']
print(re.findall('3$', 'hello egon 123'))  # ['3']

# 重复匹配:| . | * | ? | .* | .*? | + | (n,m) |
# .
print(re.findall('a.b', 'a1b'))  # ['a1b']
print(re.findall('a.b', 'a1b a*b a b aaab'))  # ['a1b', 'a*b', 'a b', 'aab']
print(re.findall('a.b', 'a\nb'))  # []
print(re.findall('a.b', 'a\nb', re.S))  # ['a\nb']
print(re.findall('a.b', 'a\nb', re.DOTALL))  # ['a\nb']同上一条意思一样

# *
print(re.findall('ab*', 'abbbbbbb'))  # ['abbbbbbb']
print(re.findall('ab*', 'a'))  # ['a']
print(re.findall('ab*', 'bbbbbbb'))  # []

# ?
print(re.findall('ab?', 'a'))  # ['a']
print(re.findall('ab?', 'abbb'))  # ['ab']
# 匹配包含所有小数在内的数字
print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']

# .*默认为贪婪匹配
print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b']

# .*?为非贪婪匹配:推荐使用
print(re.findall('a.*?b','a1b22222222b'))#['a1b']

# +
print(re.findall('ab+','a')) #[]
print(re.findall('ab+','abbb'))#['abbb']

# {n,m}
print(re.findall('ab{2}','abbb'))#['abb']
print(re.findall('ab{2,4}','abbb')) #['abbb']
print(re.findall('ab{1,}','abbb'))#'ab{1,}' ===> 'ab+'
print(re.findall('ab{0,}','abbb'))#'ab{0,}' ===> 'ab*'

# []
print(re.findall('a[1*-]b','a1b a*b a-b')) #['a1b', 'a*b', 'a-b'],[]内的都为普通字符,且如果-没有被转意的话,应放到[]额开头或结尾
print(re.findall('a[^1*-]b','a1b a*b a-b a=b'))#['a=b'],[]内的^代表的意思是取反
print(re.findall('a[0-9]b','a1b a*b a-b a=b,a2b,a9b'))#['a1b', 'a2b', 'a9b'],[]内-意思是区间内
print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) # ['aeb']
print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #['aeb', 'aEb']

#\#re.findall('a\\c','a\c')#对于正则来说a\\c确实可以匹配到a\c,但是在python解释器读取a\\c时,会发生转义,然后交给re去执行,所以抛出异常
print(re.findall(r'a\\c','a\c')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义
print(re.findall('a\\\\b','a\\b')) #同上面的意思一样,和上面的结果一样都是['a\\c']

3. re.S

import re

a = '''asdfsafhellopass:
    234455
    worldafdsf
    '''
b = re.findall('hello(.*?)world', a)
c = re.findall('hello(.*?)world', a, re.S)
print('b is ', b) # b is  []
print('c is ', c) # c is  ['pass:\n\t234455\n\t']
'''
正则表达式中,“.”的作用是匹配除“\n”以外的任何字符,也就是说,它是在一行中进行匹配。这里的“行”是以“\n”进行区分的。a字符串有每行的末尾有一个“\n”,不过它不可见。
如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始,不会跨行。而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,将“\n”当做一个普通的字符加入到这个字符串中,在整体中进行匹配。
'''

 

posted @ 2021-02-02 15:04  Avery_W  阅读(85)  评论(0)    收藏  举报