How to decipher :re.split(r"(\(\([^)]+\)\))" in the example

Joel Goldstick joel.goldstick at gmail.com
Thu Jul 10 13:05:49 EDT 2014


On Thu, Jul 10, 2014 at 11:37 AM, fl <rxjwg98 at gmail.com> wrote:
> Hi,
>
> This example is from the link:
>
> https://wiki.python.org/moin/RegularExpression
>
>
> I have thought about it quite a while without a clue yet. I notice that it uses
> double quote ", in contrast to ' which I see more often until now.

Double quotes or single quotes -- doesn't matter.

> It looks very complicated to me. Could you simplified it to a simple example?
>
You might read up first here: https://docs.python.org/2/library/re.html

If you are just new to learning python, regular expressions are not a
good place to start.  But if you insist, the page you are looking at
is more of a cheat sheet .

Try the python docs, and tutorial first.  Or google.


>
> Thanks,
>
>
>
>
>
> import re
> split_up = re.split(r"(\(\([^)]+\)\))",
>                     "This is a ((test)) of the ((emergency broadcasting station.))")
>
>
The outer parens are for grouping.  I'm not good at regexes but it
looks like it wants two open parens followed by any number of
characters that are anything but a close paren, followed by two close
parens.  So whenever it finds that pattern it splits off what is on
either side of it.


> ...which produces:
>
>
> ["This is a ", "((test))", " of the ", "((emergency broadcasting station.))" ]
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list