re模块中其它常用的方法

 1 import re 
 2 print('--------------sub-------------')
 3 phone = "2004-959-559 # 这是一个国外电话号码"
 4 # 删除字符串中的 Python 注释
 5 pattern = r'#.*$'
 6 result = re.sub(pattern,'',phone)
 7 print("电话号码是: ", result)
 8 # 删除非数字(-)的字符串
 9 num = re.sub(r'\D', "", phone)
10 print("电话号码是 : ", num)
11 # 删除注释
12 pattern = r'#\D*'
13 result = re.sub(pattern,'',phone)
14 print('sub:',result)
15 
16 print('-------------subn-------------')
17 result=re.subn(pattern,'', phone) 
18 # 返回一个元组
19 print(result) 
20 print(result[0])
21 print(result[1])
22 
23 print('-------------compile----------')
24 """
25 compile函数用于编译正则表达式,生成一个正则表达式(Pattern)对象,供match()
26 和 search() 这两个函数使用。
27 """
28 s = 'first123 line'
29 pattern = r'\w+'
30 regex = re.compile(pattern)
31 m = regex.match(s)
32 print(m)
33 
34 print('----------- findall----------')
35 s = 'first 1 second 2 third 3'
36 pattern = r'\w+'
37 result = re.findall(pattern,s)
38 print(result)
39 
40 print('----------- finditer----------')
41 # 返回的是一个迭代器
42 s = 'first 1 second 2 third 3'
43 pattern = r'\w+'
44 result = re.finditer(pattern,s)
45 print(result)
46 for i in result:
47     print(i.group(),end='\t')
48 print()
49 
50 
51 print('--------------split------------')
52 s = 'first 1 second 2 third 3'
53 pattern = r'\d+'
54 result = re.split(pattern,s)
55 print(result)
56 result = re.split(pattern,s,maxsplit=2)
57 print(result)
 1 -------------sub------------
 2 电话号码是:  2004-959-559
 3 电话号码是 :  2004959559
 4 sub: 2004-959-559
 5 ------------subn------------
 6 ('2004-959-559 ', 1)
 7 2004-959-559
 8 1
 9 -------------compile----------
10 <re.Match object; span=(0, 8), match='first123'>
11 ----------- findall----------
12 ['first', '1', 'second', '2', 'third', '3']
13 ----------- finditer----------
14 <callable_iterator object at 0x0000012D67812088>
15 first   1       second  2       third   3
16 --------------split------------
17 ['first ', ' second ', ' third ', '']
18 ['first ', ' second ', ' third 3']

 

posted @ 2020-05-18 10:32  小他_W  阅读(198)  评论(0编辑  收藏  举报