1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
import re
a = re.compile('abc'); print("模式对象:" + str(a)) b = a.search('abdabcbabab'); print('匹配到的字符串信息:'+ str(b)) c = a.match('abdabcbabab'); print("第一个匹配的字符串:" + str(c)) d = re.findall('abc', 'abdabcbabcab'); print("匹配到的字符列表:" + str(d)) e = re.sub('abc','aaa','abdabcbabcab'); print("匹配到的字符列表:" + str(e)) f = re.split(',', 'ab,bc,cd,de'); print("分割字符:" + str(f))
aa = re.search('abc', 'ABCc1B3ABC2',re.I); print("大小写不敏感:" + str(aa))
bb = re.findall('^abc', 'ABCc1B3abc2'); print("匹配开头:" + str(bb))
cc = re.findall('abc$', 'ABCc1B3abc'); print("匹配结尾:" + str(cc))
dd = re.findall('.', 'ABCc1B3abc2'); print("匹配任意字符: " +str(dd))
ee = re.findall('[abc]', 'ABCc1B3abc'); print("匹配一组单个字符:" + str(ee))
ee = re.findall('[^abc]', 'ABCc1B3abc'); print("匹配不在[]的单个字符:" + str(ee))
ff = re.findall('1*', 'ABCc1B3abc'); print("匹配0个或多个字符:" + str(ff))
ff = re.findall('1+', 'ABCc1B3abc'); print("匹配1个或多个字符:" + str(ff))
ff = re.findall('1B3?', 'ABCc1B3abc'); print("匹配0个或1个字符:" + str(ff))
ff = re.findall('c{2}', 'ABCcc1B3abc'); print("精确匹配n个前面的表达式:" + str(ff))
ff = re.findall('c{2,}', 'ABcCcc1Bccccc3abc'); print("匹配n个前面表达式:" + str(ff))
ff = re.findall('c{2,4}', 'ABcCcc1Bccc3abc'); print("匹配n到m此由前面的正则表达式定义的片段:" + str(ff))
ff = re.findall('\d', 'ABcCcc1Bccc3abc'); print("匹配一个数字字符 :" + str(ff))
ff = re.findall('\D', '123c1Bc3abc'); print("匹配一个非数字字符 :" + str(ff))
ff = re.findall('\s', '123 c1Bc 3abc'); print("匹配任何空白字符 :" + str(ff))
ff = re.findall('\S', '1 c2c 3abc'); print("匹配任何非空白字符 :" + str(ff))
ff = re.findall('\w', '1 c_A2c 3abc'); print("匹配包括下划线的任何单词字符 :" + str(ff))
ff = re.findall('\W', '1 c_2c 3abc'); print("匹配任何非单词字符 :" + str(ff))
ff = re.findall('1B3?', 'ABCc1Bx3abc'); print("匹配0个或1个字符:" + str(ff))
|