Matthias
Matthias

Reputation: 5764

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.

Upvotes: 0

Views: 375

Answers (3)

malugina
malugina

Reputation: 196

Try this:

s = '{http://datex2.eu/schema/2/2_0}nodeName'
search = re.search('{.*}(.*)',s)
print (search.group(1))

Upvotes: 2

linden2015
linden2015

Reputation: 887

Like this? Demo.

(?P<curlyStuff>\{[^}]+\})?(?P<nodeName>\w+)\b

Upvotes: 1

orip
orip

Reputation: 75457

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:]

Upvotes: 2

Related Questions