RegEx:在花括号后获取所有内容 [英] RegEx: get everything after curly braces

查看:40
本文介绍了RegEx:在花括号后获取所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从包含命名空间的 XML 文件中获得节点名称:

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 ...

感谢您的帮助!

PS:我用 Python 3 编码.

PS: I'm coding in Python 3.

推荐答案

如果你假设你想要 "}" 之后的所有内容,也可以不用正则表达式:

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

  1. 使用 rsplit - 取}"后面的内容

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

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

使用 rsplit 更高效 - 最多拆分一次

More efficiently with rsplit - split at most once

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

使用 rfind 更高效,不会分配带有我们丢弃的前缀的字符串

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

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

这篇关于RegEx:在花括号后获取所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆