How to decipher :re.split(r"(\(\([^)]+\)\))" in the example
joel.goldstick at gmail.com
Thu Jul 10 19:05:49 CEST 2014
On Thu, Jul 10, 2014 at 11:37 AM, fl <rxjwg98 at gmail.com> wrote:
> This example is from the link:
> 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.
> 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.))" ]
More information about the Python-list