您的示例数据不是 XML。你的斜杠是向后的。假设它is如果您尝试解析 XML,答案是“不要使用正则表达式”。
他们根本无法处理必要程度的递归和嵌套。
因此,考虑到这一点 - 假设您的示例数据实际上是格式良好的 XML,这是一个拼写错误,例如XML::Twig
会很方便地做到这一点:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig -> parse ( \*DATA );
#extract a single field value
print $twig -> root -> first_child_text('title'),"\n";
#get a field name
print $twig -> root -> first_child -> tag,"\n";
#can also use att() if you have attributes
print "Field names:\n";
#children() returns all the children of the current (in this case root) node
#We use map to access all, and tag to read their 'name'.
#att or trimmed_text would do other parts of the XML.
print join ( "\n", map { $_ -> tag } $twig -> root -> children );
__DATA__
<XML>
<record>DEFECT000179</record><state>Approved</state><title>Something is broken</title>
</XML>
这打印:
Something is broken
record
Field names:
record
state
title
您还拥有各种其他真正有用的工具,例如pretty_print
用于格式化输出 XML,twig_handlers
允许您在解析时操作 XML(对于purge
), cut
and paste
移动节点,以及get_xpath
让您使用 xpath 表达式根据路径和属性查找元素。
编辑:根据评论,如果您确实想从以下位置提取数据:
</something>
你的事情出了问题的是.*
是贪婪的。您要么需要使用否定匹配 - 例如:
m,</[^>]>,g
或者非贪婪匹配:
m,</(.*?)>,g
哦,鉴于你有一个反斜杠 - 你需要转义它:
my $firstLineOfXMLFile = '<record>DEFECT000179<\record><state>Approved<\state><title>Something is broken<\title>';
my @fieldNames = $firstLineOfXMLFile =~ m(<\\(.*?)>)g;
print @fieldNames;
会成功的。 (但是说实话,故意创建一些看起来像 XML 但实际上不是 XML 的东西确实是一件很糟糕的事情)