python正则表达式

写了部分代码,测试各个表达式的含义

有点烦,我想,这样的机械性工作完全可以用List或是字典,循环完成的。可惜我还没怎么学过

以后会把另一半的测试补全

 1 #!use/bin/python
 2 #coding:utf-8
 3 
 4 import re
 5 
 6 str= "http://www.cnblogs.com/sub2020/p/7920845.html";
 7 print str
 8 
 9 #re.split按照能够匹配的子串将string分割后返回列表
10 # . 代表任意字符
11 pattern =re.compile(r'.')
12 print 're.compile(.) :',re.split(pattern,str)
13 print 're.split(. 2) :',re.split(pattern,str,2)
14 
15 #\    对下一字符取非(通常是普通变特殊,特殊变普通)
16 pattern =re.compile(r'\\')
17 print 're.compile(\\)  :',re.split(pattern,str)
18 print 're.split(\\ 2) :',re.split(pattern,str,2)
19 
20 #[] 匹配内部的任一字符或子表达式
21 pattern =re.compile(r'[l]')
22 print 're.compile([l])  :',re.split(pattern,str)
23 print 're.split(l 2) :',re.split(pattern,str,2)
24 
25 #预定义字符集
26 #\d = [0-9] :0123456789任意之一
27 pattern =re.compile(r'\d+')
28 print 're.split(\d+)  :',re.split(pattern,str)
29 print 're.split(\d+ 2) :',re.split(pattern,str,2)
30 
31 #\D :等同于[^0-9]匹配非数字
32 pattern =re.compile(r'\D')
33 print 're.compile(\D)  :',re.split(pattern,str)
34 print 're.split(\D 2) :',re.split(pattern,str,2)
35 
36 #\s :空白字符
37 pattern =re.compile(r'\s')
38 print 're.compile(\s)  :',re.split(pattern,str)
39 print 're.split(\s 2) :',re.split(pattern,str,2)
40 
41 #\S :非空白字符
42 pattern =re.compile(r'\S')
43 print 're.compile\S)  :',re.split(pattern,str)
44 print 're.split(\S 2) :',re.split(pattern,str,2)
45 
46 #\w     :等同于[a-z0-9A-Z_]匹配大小写字母、数字和下划线
47 pattern =re.compile(r'\w')
48 print 're.compile(\w)  :',re.split(pattern,str)
49 print 're.split(\w 2) :',re.split(pattern,str,2)
50 
51 #\W     :等同于[^a-z0-9A-Z_]等同于上一条取非
52 pattern =re.compile(r'\W')
53 print 're.compile(\W)  :',re.split(pattern,str)
54 print 're.split(\W 2) :',re.split(pattern,str,2)
55 
56 #数量词
57 # *    匹配前面的字符或者子表达式0次或多次
58 pattern =re.compile(r'b*')
59 print 're.compile(b*)      :',re.split(pattern,str)
60 print 're.split(b* 2)      :',re.split(pattern,str,2)
61 
62 
63 # +    匹配前一个字符或子表达式一次或多次
64 pattern =re.compile(r'b+')
65 print 're.compile(b+)      :',re.split(pattern,str)
66 print 're.split(b+ 2)      :',re.split(pattern,str,2)
67 
68 
69 # ?    匹配前一个字符或子表达式0次或1次重复
70 pattern =re.compile(r'b?')
71 print 're.compile(b?)      :',re.split(pattern,str)
72 print 're.split(b? 2)      :',re.split(pattern,str,2)
73 
74 # {n}    匹配前一个字符或子表达式
75 pattern =re.compile(r'b{3}')
76 print 're.compile(b{3})    :',re.split(pattern,str)
77 print 're.split(b{3} 2)    :',re.split(pattern,str,2)
78 
79 
80 # {m,n}    匹配前一个字符或子表达式至少m次至多n次
81 pattern =re.compile(r'b{2,4}')
82 print 're.compile(b{2,4})  :',re.split(pattern,str)
83 print 're.split(b{2,4} 2)  :',re.split(pattern,str,2)
84 
85 # *?    惰性匹配上一个
86 pattern =re.compile(r'b*?')
87 print 're.compile(b*?)     :',re.split(pattern,str)
88 print 're.split(b*? 2)     :',re.split(pattern,str,2)

Output:

 1 http://www.cnblogs.com/sub2020/p/7920845.html
 2 re.compile(.) : ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
 3 re.split(. 2) : ['', '', 'tp://www.cnblogs.com/sub2020/p/7920845.html']
 4 re.compile(\)  : ['http://www.cnblogs.com/sub2020/p/7920845.html']
 5 re.split(\ 2) : ['http://www.cnblogs.com/sub2020/p/7920845.html']
 6 re.compile([l])  : ['http://www.cnb', 'ogs.com/sub2020/p/7920845.htm', '']
 7 re.split(l 2) : ['http://www.cnb', 'ogs.com/sub2020/p/7920845.htm', '']
 8 re.split(\d+)  : ['http://www.cnblogs.com/sub', '/p/', '.html']
 9 re.split(\d+ 2) : ['http://www.cnblogs.com/sub', '/p/', '.html']
10 re.compile(\D)  : ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2020', '', '', '7920845', '', '', '', '', '']
11 re.split(\D 2) : ['', '', 'tp://www.cnblogs.com/sub2020/p/7920845.html']
12 re.compile(\s)  : ['http://www.cnblogs.com/sub2020/p/7920845.html']
13 re.split(\s 2) : ['http://www.cnblogs.com/sub2020/p/7920845.html']
14 re.compile\S)  : ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
15 re.split(\S 2) : ['', '', 'tp://www.cnblogs.com/sub2020/p/7920845.html']
16 re.compile(\w)  : ['', '', '', '', '://', '', '', '.', '', '', '', '', '', '', '.', '', '', '/', '', '', '', '', '', '', '/', '/', '', '', '', '', '', '', '.', '', '', '', '']
17 re.split(\w 2) : ['', '', 'tp://www.cnblogs.com/sub2020/p/7920845.html']
18 re.compile(\W)  : ['http', '', '', 'www', 'cnblogs', 'com', 'sub2020', 'p', '7920845', 'html']
19 re.split(\W 2) : ['http', '', '/www.cnblogs.com/sub2020/p/7920845.html']
20 re.compile(b*)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
21 re.split(b* 2)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
22 re.compile(b+)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
23 re.split(b+ 2)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
24 re.compile(b?)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
25 re.split(b? 2)      : ['http://www.cn', 'logs.com/su', '2020/p/7920845.html']
26 re.compile(b{3})    : ['http://www.cnblogs.com/sub2020/p/7920845.html']
27 re.split(b{3} 2)    : ['http://www.cnblogs.com/sub2020/p/7920845.html']
28 re.compile(b{2,4})  : ['http://www.cnblogs.com/sub2020/p/7920845.html']
29 re.split(b{2,4} 2)  : ['http://www.cnblogs.com/sub2020/p/7920845.html']
30 re.compile(b*?)     : ['http://www.cnblogs.com/sub2020/p/7920845.html']
31 re.split(b*? 2)     : ['http://www.cnblogs.com/sub2020/p/7920845.html']
32 
33 ***Repl Closed***

 

 

 

 

 

 

 

 

 

 

 

 

quote:http://cuiqingcai.com/977.html

posted on 2017-11-29 11:15  sub2020  阅读(148)  评论(0编辑  收藏  举报

导航