Search code examples
pythonregextrim

RegEx: get everything after curly braces


I got node names from an XML file that contain a namespace:

{http://datex2.eu/schema/2/2_0}nodeName

From this I would like to trim the namespace, which is in curly braces. So the result should be the node name only. Could be either matching all contents in curly braces with a RegEx and then removing them from the string. Or matching the content after the braces directly. But I'm too stupid to get the Regex right ...

Thanks for any help!

PS: I'm coding in Python 3.


Solution

  • Can be done without regex simply if you assume you want everything after the "}":

    1. With rsplit - take what's after the "}"

      s.rsplit("}")[-1]

    2. More efficiently with rsplit - split at most once

      s.rsplit("}", 1)[-1]

    3. More efficient with rfind, doesn't allocate a string with the prefix we're throwing away

      s[s.rfind("}")+1:]