re模块详解
Posted 朝阳的向日葵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了re模块详解相关的知识,希望对你有一定的参考价值。
1 #!/usr/bin/env python
2 #-*- coding:UTF-8 -*-
3 #####################################################
4 # Author: sunfx [email protected]
5 # Last modified: 2014/11/18
6 # Filename: re.py
7 # Q Q 群: 236147801
8 #####################################################
9
10 import re
11
12 #1.查找文本中的字符
13
14 pattern = ‘this‘
15 text = ‘Does this text match the pattern?‘
16
17 match = re.search(pattern,text)
18
19 s = match.start()
20 e = match.end()
21
22 print ‘Found "%s"\nin "%s"\nfrom %d to %d ("%s")‘ % 23 (match.re.pattern,match.string,s,e,text[s:e])
24
25 ‘‘‘
26 match.re.pattern 要匹配的内容
27 match.string 匹配的字符
28 s 匹配到内容开始索引
29 d 匹配到内容结束索引
30 text[s:e] 匹配字符
31 ‘‘‘
32
33 #2.编译表达式
34
35 regexes = [ re.compile(p)
36 for p in [‘this‘,‘that‘]
37 ] #把字符转换Regexobject格式
38
39
40
41 print ‘Text: %r\n‘ % text #输出text内容
42
43 for regex in regexes:
44
45 print ‘Seeking "%s"->‘ % regex.pattern, #regex.pattern 要匹配的字符
46
47 if regex.search(text): #在text中搜索this or that
48
49 print ‘match!‘
50
51 else:
52
53 print ‘no match‘
54
55 #3.多重匹配
56
57 text = ‘abbaaabbbbaaaaa‘
58
59 pattern = ‘ab‘
60
61 for match in re.findall(pattern,text):
62
63 print ‘Found: "%s"‘ % match
64
65 #findall 直接返回字符串
66
67
68 for match in re.finditer(pattern,text):
69 s = match.start()
70 e = match.end()
71 print ‘Found "%s" at %d:%d‘ % (text[s:e],s,e)
72
73 #finditer 返回原输入文字在字符串的位置
74
75 #4.模式语法
76
77 def test_patterns(text,patterns=[]):
78
79 for pattern,desc in patterns:
80 print ‘Pattern %r (%s) \n‘ %(pattern,desc)
81 print ‘ %r‘ % text
82 for match in re.finditer(pattern,text):
83 s = match.start()
84 e = match.end()
85 substr = text[s:e] #匹配到的字符
86 n_backslashes = text[:s].count(‘\\‘) #查找文本:s坐标之前的包含多少\ 87 prefix = ‘.‘ * ( s + n_backslashes )
88 print ‘ %s%r‘ % (prefix,substr)
89 print
90 return
91