Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 用于在标记中查找字符串的正则表达式_Python_Regex_Xml_Expression - Fatal编程技术网

Python 用于在标记中查找字符串的正则表达式

Python 用于在标记中查找字符串的正则表达式,python,regex,xml,expression,Python,Regex,Xml,Expression,对于如何生成正则表达式以查找和中的所有字符串,有人提供了一些指导吗?下面的代码中有3种情况。我必须使用正则表达式返回列表中time和/time之间的3个字符串 <tabular> <time from="2015-09-23T23:00:00" to="2015-09-24T00:00:00" period="3"> <!-- Valid from 2015-09-23T23:00:00 to 2015-09-24T00:00:00 -->

对于如何生成正则表达式以查找
中的所有字符串,有人提供了一些指导吗?下面的代码中有3种情况。我必须使用正则表达式返回列表中time和/time之间的3个字符串

<tabular>
  <time from="2015-09-23T23:00:00" to="2015-09-24T00:00:00" period="3">
    <!-- Valid from 2015-09-23T23:00:00 to 2015-09-24T00:00:00 -->
    <symbol number="4" numberEx="4" name="Cloudy" var="04" />
    <precipitation value="0" />
    <!-- Valid at 2015-09-23T23:00:00 -->
    <windDirection deg="118.5" code="ESE" name="East-southeast" />
    <windSpeed mps="1.2" name="Light air" />
    <temperature unit="celsius" value="12" />
    <pressure unit="hPa" value="1010.4" />
  </time>
  <time from="2015-09-24T00:00:00" to="2015-09-24T06:00:00" period="0">
    <!-- Valid from 2015-09-24T00:00:00 to 2015-09-24T06:00:00 -->
    <symbol number="4" numberEx="4" name="Cloudy" var="04" />
    <precipitation value="0" />
    <!-- Valid at 2015-09-24T00:00:00 -->
    <windDirection deg="94.7" code="E" name="East" />
    <windSpeed mps="1.9" name="Light breeze" />
    <temperature unit="celsius" value="12" />
    <pressure unit="hPa" value="1010.4" />
  </time>
  <time from="2015-09-24T06:00:00" to="2015-09-24T12:00:00" period="1">
    <!-- Valid from 2015-09-24T06:00:00 to 2015-09-24T12:00:00 -->
    <symbol number="4" numberEx="4" name="Cloudy" var="04" />
    <precipitation value="0" minvalue="0" maxvalue="0.3" />
    <!-- Valid at 2015-09-24T06:00:00 -->
    <windDirection deg="122.9" code="ESE" name="East-southeast" />
    <windSpeed mps="2.6" name="Light breeze" />
    <temperature unit="celsius" value="12" />
    <pressure unit="hPa" value="1009.3" />
  </time>
</tabular>

它是一个xml文件,所以为什么不使用
XPATH
而不是regex呢。为此,您可以使用名为
lxml
的python库,因为它支持
XPATH
语言。我不知道您的确切用例,但示例代码应该是这样的:-

from lxml import etree

xml_doc = etree.fromstring(xml_string) # assuming xml_string is xml_content

xpath_expression = '/tabular/time/text()' # change it according to your use case

data = xml_doc.xpath(xpath_expression)
试试这个

re.findall(r']*>.*?','XMLSTRING',re.DOTALL)
试试看

<time[^>]*>(.*?)<\/time>
]*>(**?)
它返回三个匹配组。检查它(注意
全局
单行
标志)


这将获取每个时间元素的内容,如下所示:

re.findall("<time[^>]*>(.*?)<\/time>",xmldata, re.DOTALL)
与:

从lxml导入etree
xmlString=''
...'''
tree=etree.fromstring(xmlString)
res=[]
对于tree.xpath('//tablar/time')中的timeNd:
res.append(“”.join([etree.tostring(node)用于timeNd中的节点])
打印(res)

XPath查询
//tabular/time
选择所有
time
节点,然后对每个节点连接子节点字符串,并将结果字符串附加到列表
res

中。由于XML不是一种常规语言(这是计算机科学中的一个技术术语),因此无法执行该查询。任何尝试都会捕获一些您不想捕获的字符串(例如,设计用于混淆正则表达式匹配器的XML注释),或者无法捕获一些您应该匹配的字符串(例如,在您不希望的地方包含注释的元素)。要处理XML,请始终使用XML解析器。

三个字符串是什么意思?对不起,我要匹配三个匹配项。是否希望所有时间元素的XML内容都作为字符串?无论如何,我强烈建议使用适当的xml库,如
lxml
,而不是正则表达式。是的,正确。我尝试使用re.findall(),但尝试时没有找到匹配项。我想用正则表达式来解决这个问题。这几乎奏效了,但它只是返回了列表中的整个字符串。我需要分隔字符串,因此在本例中,它应该返回一个包含3个元素的列表。编辑以包含“?”以使其不贪婪。抱歉,请参阅@clasG的答案,其中有括号用于捕获匹配项
re.findall("<time[^>]*>.*?<\/time>",xmldata, re.DOTALL)
from lxml import etree

xmlString = '''<tabular>
...'''

tree = etree.fromstring(xmlString)

res = []

for timeNd in tree.xpath('//tabular/time'): 
    res.append(''.join([etree.tostring(node) for node in timeNd]))

print(res)