0

Why does this work:

string = 'N{P}[ST]{P}'

>>> import re
>>> re.split(r"[\[\]]",  string)
>>> ['N{P}', 'ST', '{P}']

But this don't?

>>> re.split(r"{\{\}}", string)
1
  • I highly recommend you regexr, it's a wonderful tool for learning regular expressions. Commented Aug 28, 2016 at 16:14

2 Answers 2

1

You have to do this:

re.split(r"[{}]", string)

r"{\{\}}" is a special re syntax to repeat groups (ex: (ab){1,3} matches ab, abab or ababab) but not the character range (note that you don't have to escape the curly braces in a character range). (I admit I don't know what your strange regex is supposed to do specially in the re.split context, but not what you want :))

Sign up to request clarification or add additional context in comments.

Comments

0

{ and } are meta-characters in regular expressions and should be escaped or put into a character class
{\{\}} - means just escaped curly braces within unescaped

1 Comment

just out of curiosity, do you have an idea of what it does? (since it doesn't crash)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.