Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; '"""': 0.05; 'subject:Question': 0.07; 'itself,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'replace': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; "skip:' 10": 0.30; 'could': 0.32; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'decided': 0.65; 'skip:$ 10': 0.66; 'frank': 0.75 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Regex Question Date: Sat, 18 Aug 2012 17:48:42 +0200 Organization: None References: <385e732e-1c02-4dd0-ab12-b92890bbed66@o3g2000yqp.googlegroups.com> <502fa524$0$29978$c3e8da3$5496439d@news.astraweb.com> <79aaa167-296a-4a0c-8a06-c4e67cf53597@j19g2000yqi.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508492ab.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345304932 news.xs4all.nl 6885 [2001:888:2000:d::a6]:37161 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27302 Frank Koshti wrote: > I need to match, process and replace $foo(x=3), knowing that (x=3) is > optional, and the token might appear simply as $foo. > > To do this, I decided to use: > > re.compile('\$\w*\(?.*?\)').findall(mystring) > > the issue with this is it doesn't match $foo by itself, and requires > there to be () at the end. >>> s = """ ...

$foo1

...

$foo2()

...

$foo3(anything could go here)

... """ >>> re.compile("(\$\w+(?:\(.*?\))?)").findall(s) ['$foo1', '$foo2()', '$foo3(anything could go here)']