@@ -65,11 +65,105 @@ def ch2_3():
6565 print (data_file )
6666
6767
68+ def ch2_4 ():
69+ '''
70+ 2.4 文本模式的匹配和查找
71+ 使用正则
72+ '''
73+
74+ print ("\n ch2_4:" )
75+
76+ text1 = "02/11/2017"
77+ text2 = "02/11/2017 tomorrow is 02/12/2017"
78+
79+ import re
80+ if re .match (r"\d+/\d+/\d+" , text1 ):
81+ print ("yes" )
82+ else :
83+ print ("no" )
84+
85+ date_p = re .compile (r"\d+/\d+/\d+" )
86+ if date_p .match (text1 ):
87+ print ("yes" )
88+ else :
89+ print ("no" )
90+
91+ result = date_p .findall (text2 )
92+ print (result )
93+
94+ #增加捕获组 多次匹配或者查找,建议先编译
95+ date_p = re .compile (r"(\d+)/(\d+)/(\d+)" )
96+ result = date_p .findall (text2 )
97+ for month ,day , year in result :
98+ print ("{}-{}-{}" .format (year ,month ,day ))
99+
100+
101+ def ch2_5 ():
102+ '''
103+ 2.5 对字符串中的文本做查找和替换
104+
105+ '''
106+
107+ print ("\n ch2_5:" )
108+
109+ text1 = "Today is 02/11/2017, 春节是02/26/2017"
110+
111+ import re
112+
113+ #先匹配,后替换
114+ result = re .sub (r"(\d+)/(\d+)/(\d+)" ,r"\3-\1-\2" , text1 )
115+ print (result )
116+
117+ data_p = re .compile (r"(\d+)/(\d+)/(\d+)" )
118+
119+ from calendar import month_abbr
120+
121+ def change_date (m ):
122+ mon_name = month_abbr [int (m .group (1 ))]
123+ return '{} {} {}' .format (m .group (2 ),mon_name ,m .group (3 ))
124+
125+ result ,count = data_p .subn (change_date , text1 )
126+ print ("结果:" ,result , "\n 总共替换:" , count )
127+
128+
129+
130+ def ch2_6 ():
131+ '''
132+ 2.6 以不区分大小写的方式对文本做检查和替换
133+ 使用re.IGNORECASE
134+ '''
135+ print ("\n ch2_6:" )
136+
137+ text = "UPPER PYTHON, lower python, Mixed Python"
138+
139+ import re
140+ result = re .findall (r"python" , text , flags = re .IGNORECASE )
141+ print (result )
142+
143+ #替换成相同的格式
144+ result = re .sub ("python" , "snake" , text , flags = re .IGNORECASE )
145+ print (result ) #注意snake是全部小写
146+
147+ def matchcase (word ):
148+ def replace (m ):
149+ text = m .group ()
150+ if text .isupper ():
151+ return word .upper ()
152+ elif text .islower ():
153+ return word .lower ()
154+ elif text [0 ].isupper ():
155+ return word .capitalize ()
156+ else :
157+ return word
158+ return replace
159+
160+ result = re .sub ("python" , matchcase ("snake" ), text , flags = re .IGNORECASE )
161+ print (result )
68162
69163
70164def main ():
71165
72- for i in range (1 ,4 ):
166+ for i in range (1 ,7 ):
73167 func = 'ch2_%d()' % (i )
74168 exec (func )
75169
0 commit comments