Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38866

Re: how to right the regular expression ?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.04; 'patterns': 0.04; 'attribute': 0.05; 'pat': 0.05; '-*-': 0.07; 'utf-8': 0.07; 'python': 0.09; 'coding:': 0.09; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'literals': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'output?': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'string': 0.17; 'wrote:': 0.17; 'skip:p 30': 0.20; 'import': 0.21; 'example': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'raw': 0.27; "doesn't": 0.28; 'optional': 0.29; 'skip:( 40': 0.30; 'subject: ?': 0.30; 'code': 0.31; 'file': 0.32; 'received:84': 0.32; 'print': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'code:': 0.33; 'recommended': 0.33; 'but': 0.36; 'subject:: ': 0.38; 'skip:( 30': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'url:ip addr': 0.62; 'skip:n 10': 0.63; 'here': 0.65; 'header:Reply-To:1': 0.68; '8bit%:100': 0.70; '8bit%:92': 0.70; 'reply-to:no real name:2**0': 0.72; '8bit%:45': 0.84; '8bit%:65': 0.84; '8bit%:66': 0.84; 'reply- to:addr:python.org': 0.84; 'url:177': 0.84; 'url:202': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=l2ldf9mSM50A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=yrlwa_sxtEwA:10 a=_CEnuSUOAAAA:8 a=u1Y1aK5dOgtMhRN7vxUA:9 a=QEXdDO2ut3YA:10 a=sSxwxQke6HgA:10 a=Zhm7LKGWaE8A:10 a=J_3VfmUUkkMA:10 a=mixiRZqWv8AA:10 a=0mPxtDiXJLIA:10 a=qiWi7Edwir4A:10 a=NWVoK91CQyQA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Thu, 14 Feb 2013 15:43:42 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: how to right the regular expression ?
References <227f6014.405c.13cd90da3d4.Coremail.mailtomanage@163.com>
In-Reply-To <227f6014.405c.13cd90da3d4.Coremail.mailtomanage@163.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1769.1360856630.2939.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360856630 news.xs4all.nl 6935 [2001:888:2000:d::a6]:36587
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38866

Show key headers only | View raw


On 2013-02-14 14:13, python wrote:
> 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?
>
The problem is that the regex makes '(\(.+\))' mandatory, but example 7
doesn't match it.

You can make it optional by wrapping it in a non-capturing group
(?:...), like this:

pat = r'([a-z].+?\s)(.+)(?:(\(.+\)))?'

Also, it's highly recommended that you use raw string literals
(r'...') when writing regex patterns and replacements.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to right the regular expression ? MRAB <python@mrabarnett.plus.com> - 2013-02-14 15:43 +0000

csiph-web