Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在xml字符串中查找模式_Python_Regex - Fatal编程技术网

Python 在xml字符串中查找模式

Python 在xml字符串中查找模式,python,regex,Python,Regex,我的xml文件中有以下xml标记,如下所示 ''' “www.altruvest.org或www.Boardmatch.org) ''' 在上面的标记pd中:unicode标记位于目标的文本值内。我想创建正则表达式模式来查找这样的标记,在python中标记位于文本内 是否有人可以帮助创建此模式?编辑答案: >>> s = r'"<pd:link scheme="http://www.w3.org/1999/xhtml" target="www.altruvest.org

我的xml文件中有以下xml标记,如下所示 '''

“www.altruvest.org或www.Boardmatch.org)
'''

在上面的标记pd中:unicode标记位于目标的文本值内。我想创建正则表达式模式来查找这样的标记,在python中标记位于文本内

是否有人可以帮助创建此模式?

编辑答案:

>>> s = r'"<pd:link scheme="http://www.w3.org/1999/xhtml" target="www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org">www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org</pd:link>"'
>>> import re
>>> r = re.search(r'=".*?(<pd:unicode ch="\d+"/>).*?"', s, re.DOTALL)
>>> r.groups()
('<pd:unicode ch="2014"/>',)
>>s=r'“www.altruvest.org或www.Boardmatch.org”
>>>进口稀土
>>>r=re.search(r'=“*?()*”,s,re.DOTALL)
>>>r.团体()
('',)
上面所做的是匹配
pd:unicode
标记,当它们前面是
=“
,后面是
re.DOTALL
忽略换行符(将它们视为普通字符)

请记住,您要做的是解析XML,您应该使用xmlparser(参见示例或更一般的讨论)来解析XML,而不是正则表达式。使用正则表达式精确解析XML实际上是错误的,因此上面的正则表达式可能会产生误报或漏报一些真实的结果


如果你不想使用一个完整的XML解析器,你可以考虑一些类似的东西。

不清楚你的标签是什么样的。请提供一个更全面的输入例子,并试图准确地描述你想要的标签。如果你不知道正则表达式的语言,试着用英语来描述它,比如:“pig”或“dog”两个字在一行末尾被方括号包围。没有人能理解您的问题,因为您没有格式化代码,因此您放置的标记是不可见的。请阅读本网站常见问题解答并学习使用标记(SO的格式化语法)。大约需要一分钟时间!!不,如果
标记在文本中,如上面的pd:unicode标记在文本值内,我想查找
。@Yogesh-没有收到您的回复。编辑后的答案…是否回答了您的问题?如果没有,请提供反馈,如果是,请标记为已接受!:o