Need help in Python regular expression

meryl silverburgh.meryl at gmail.com
Fri Jun 12 01:20:24 EDT 2009


On Jun 11, 9:41 pm, "Mark Tolonen" <metolone+gm... at gmail.com> wrote:
> "meryl" <silverburgh.me... at gmail.com> wrote in message
>
> news:2d4d8624-043b-4f5f-ae2d-bf73bca3d51b at p6g2000pre.googlegroups.com...
>
>
>
>
>
> > Hi,
>
> > I have this regular expression
> > blockRE = re.compile(".*RenderBlock {\w+}")
>
> > it works if my source is "RenderBlock {CENTER}".
>
> > But I want it to work with
> > 1. RenderTable {TABLE}
>
> > So i change the regexp to re.compile(".*Render[Block|Table] {\w+}"),
> > but that breaks everything
>
> > 2. RenderBlock (CENTER)
>
> > So I change the regexp to re.compile(".*RenderBlock {|\(\w+}|\)"),
> > that also breaks everything
>
> > Can you please tell me how to change my reg exp so that I can support
> > all 3 cases:
> > RenderTable {TABLE}
> > RenderBlock (CENTER)
> > RenderBlock {CENTER}
>
> [abcd] syntax matches a single character from the set.  Use non-grouping
> parentheses instead:
>
> -----------------------code----------------------
> import re
> pat = re.compile(r'Render(?:Block|Table) (?:\(\w+\)|{\w+})')
>
> testdata = '''\
> RenderTable {TABLE}
> RenderBlock (CENTER)
> RenderBlock {CENTER}
> RenderTable {TABLE)      #shouldn't match
> '''
>
> print pat.findall(testdata)
> ---------------------------------------------------
>
> Result:
>
> ['RenderTable {TABLE}', 'RenderBlock (CENTER)', 'RenderBlock {CENTER}']
>
> -Mark

Thanks for both of your help. How can i modify the RegExp so that
both
RenderTable {TABLE}
and
RenderTable {TABLE} [text with a-zA-Z=SPACE0-9]
will match

I try adding ".*" at the end , but it ends up just matching the second
one.

Thanks again.



More information about the Python-list mailing list