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


Groups > comp.lang.python > #27330

Re: Regex Question

From Frank Koshti <frank.koshti@gmail.com>
Newsgroups comp.lang.python
Subject Re: Regex Question
Date 2012-08-18 13:18 -0700
Organization http://groups.google.com
Message-ID <a3d60340-be24-4e6e-ac02-b77631f6a115@z6g2000vbc.googlegroups.com> (permalink)
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> <qota9xsqieq.fsf@ruuvi.it.helsinki.fi>

Show all headers | View raw


On Aug 18, 12:22 pm, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> Frank Koshti writes:
> > not always placed in HTML, and even in HTML, they may appear in
> > strange places, such as <h1 $foo(x=3)>Hello</h1>. My specific issue
> > is 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.
>
> Adding a ? after the meant-to-be-optional expression would let the
> regex engine know what you want. You can also separate the mandatory
> and the optional part in the regex to receive pairs as matches. The
> test program below prints this:
>
> >$foo()$foo(bar=3)$$$foo($)$foo($bar(v=0))etc</htm
>
> ('$foo', '')
> ('$foo', '(bar=3)')
> ('$foo', '($)')
> ('$foo', '')
> ('$bar', '(v=0)')
>
> Here is the program:
>
> import re
>
> def grab(text):
>     p = re.compile(r'([$]\w+)([(][^()]+[)])?')
>     return re.findall(p, text)
>
> def test(html):
>     print(html)
>     for hit in grab(html):
>         print(hit)
>
> if __name__ == '__main__':
>     test('>$foo()$foo(bar=3)$$$foo($)$foo($bar(v=0))etc</htm')

You read my mind. I didn't even know that's possible. Thank you-

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


Thread

Regex Question Frank Koshti <frank.koshti@gmail.com> - 2012-08-17 21:41 -0700
  Re: Regex Question Chris Angelico <rosuav@gmail.com> - 2012-08-18 15:42 +1000
  Re: Regex Question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-18 11:50 +0100
  Re: Regex Question Roy Smith <roy@panix.com> - 2012-08-18 09:08 -0400
    Re: Regex Question Frank Koshti <frank.koshti@gmail.com> - 2012-08-18 07:21 -0700
  Re: Regex Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-18 14:22 +0000
    Re: Regex Question Frank Koshti <frank.koshti@gmail.com> - 2012-08-18 07:53 -0700
      Re: Regex Question Peter Otten <__peter__@web.de> - 2012-08-18 17:48 +0200
        Re: Regex Question Frank Koshti <frank.koshti@gmail.com> - 2012-08-18 08:56 -0700
      Re: Regex Question Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-08-18 17:50 +0200
      Re: Regex Question Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-08-18 19:22 +0300
        Re: Regex Question Frank Koshti <frank.koshti@gmail.com> - 2012-08-18 13:18 -0700
    Re: Regex Question python@bdurham.com - 2012-08-18 12:36 -0400
  Re: Regex Question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-18 13:30 -0400

csiph-web