how to right the regular expression ?

python mailtomanage at 163.com
Thu Feb 14 09:13:35 EST 2013


my tv.txt is :  
http://202.177.192.119/radio5 香港电台第五台(可于Totem/VLC/MPlayer播放)  
http://202.177.192.119/radio35 香港电台第五台(DAB版,可于Totem/VLC/MPlayer播放)  
http://202.177.192.119/radiopth 香港电台普通话台(可于Totem/VLC/MPlayer播放)  
http://202.177.192.119/radio31 香港电台普通话台(DAB版,可于Totem/VLC/MPlayer播放)  
octoshape:rthk.ch1 香港电台第一台(粤)  
octoshape:rthk.ch2 香港电台第二台(粤)  
octoshape:rthk.ch6 香港电台普通话台  
octoshape:rthk.ch3 香港电台第三台(英)  
  
  
what i want to get the result is   
1group is  http://202.177.192.119/radio5  2group is  香港电台第五台 3group is  (可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radio35  2group is  香港电台第五台 3group is  (DAB版,可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radiopth  2group is  香港电台普通话台 3group is  (可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radio31  2group is  香港电台普通话台 3group is  (DAB版,可于Totem/VLC/MPlayer播放)  
1group is  octoshape:rthk.ch1  2group is  香港电台第一台 3group is  (粤)  
1group is  octoshape:rthk.ch2  2group is  香港电台第二台 3group is  (粤)  
1group is  octoshape:rthk.ch6  2group is  香港电台普通话台 3group is  none  
1group is  octoshape:rthk.ch3  2group is  香港电台第三台 3group is  (英)  
  
here is my code:  
# -*- coding: utf-8 -*-  
import re  
rfile=open("tv.txt","r")  
pat='([a-z].+?\s)(.+)(\(.+\))'  
for  line in  rfile.readlines():  
    Match=re.match(pat,line)  
    print "1group is ",Match.group(1),"2group is ",Match.group(2),"3group is ",Match.group(3)  
rfile.close()  
  
the output is :  
1group is  http://202.177.192.119/radio5  2group is  香港电台第五台 3group is  (可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radio35  2group is  香港电台第五台 3group is  (DAB版,可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radiopth  2group is  香港电台普通话台 3group is  (可于Totem/VLC/MPlayer播放)  
1group is  http://202.177.192.119/radio31  2group is  香港电台普通话台 3group is  (DAB版,可于Totem/VLC/MPlayer播放)  
1group is  octoshape:rthk.ch1  2group is  香港电台第一台 3group is  (粤)  
1group is  octoshape:rthk.ch2  2group is  香港电台第二台 3group is  (粤)  
1group is   
Traceback (most recent call last):  
  File "tv.py", line 7, in <module>  
    print "1group is ",Match.group(1),"2group is ",Match.group(2),"3group is ",Match.group(3)  
AttributeError: 'NoneType' object has no attribute 'group'  
  
how to revise my code to get the output? 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130214/35f3e6ce/attachment.html>


More information about the Python-list mailing list