Regex 逻辑在perl中不起作用
我有一个如下所示的xml数据。我想对这些数据做一些处理。每当“entry”标记中缺少“colname”属性时,我的代码都应该插入该属性,该属性取“tgroup”标记中“cols”属性的值Regex 逻辑在perl中不起作用,regex,perl,Regex,Perl,我有一个如下所示的xml数据。我想对这些数据做一些处理。每当“entry”标记中缺少“colname”属性时,我的代码都应该插入该属性,该属性取“tgroup”标记中“cols”属性的值 <tbl ID="I78"> <table colsep="0" frame="none" rowsep="0"> <tgroup cols="4"> <tbody valign="top"> <row> <entry>i.</ent
<tbl ID="I78">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody valign="top">
<row>
<entry>i.</entry>
<entry>181.10</entry>
<entry>An inmate shall comply with the dispositions imposed by a hearing officer in a Tier I, Tier II and Tier III hearings.</entry>
<entry>I, II, III</entry>
</row>
</tbody>
</tgroup>
</table>
</tbl>
<tbl ID="I93">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody>
<row>
<entry align="center"><ital>Pledge number</ital></entry>
<entry align="center"><ital>Date</ital></entry>
<entry align="center"><ital>R</ital></entry>
<entry><ital>A or S</ital></entry>
</row>
<row>
<entry><ital>Disposition column</ital></entry>
<entry>(<ital>Renewed</ital>)</entry>
<entry>(<ital>Renewed</ital>)</entry>
</row>
<row>
<entry>(<ital>Auction Sale</ital>)</entry>
</row>
</tbody>
</tgroup>
<eos></eos>
</table>
<eop></eop>
</tbl>
我的代码如下所示:
foreach $line (@data){
if($line =~ /<tgroup(.*?)cols=\"(.*?)\">/i){
$colcount=$2;
print "\nTgroup tag found... no of cols are $colcount";
}
$templine=$line;
my $temp2line;
while($templine=~ /<tbody(.*?)>(.*?)<\/tbody>/){
$temp2line=$2;
while($temp2line=~ /<row>(.*?)<\/row>/){
$rowdata=$1;
$rowdataforfinalreplacing=$rowdata;
$temprowdata=$rowdata;
while($rowdata=~/<entry align="center">/i){
for ($i; $i<= $colcount; $i++){
$temprowdata=~s/<entry align="center">/<entry align="center" colname=\"$i\">/i;
print "\ni value :$i";
}
$rowdata=~s/<entry(.*?)<\/entry>//;
}
while($rowdata=~/<entry>/i){
for (my $i=1; $i<= $colcount; $i++){
$temprowdata=~s/<entry>/<entry colname=\"$i\">/i;
}
$rowdata=~s/<entry>(.*?)<\/entry>//;
}
$temp2line=~s/<row>(.*?)<\/row>//i;
$line=~s/$rowdataforfinalreplacing/$temprowdata/sgi;
}
}
问题是,当两行同时出现时,只有一行得到更新。当我调试时,我发现这些值得到了正确的更新,但是在写入输出文件时,它们被忽略了。我无法在我的代码中找到控件出错的地方。非常感谢您在这方面的任何帮助。提前谢谢
下面是代码的输出。。突出显示的数据未得到更新
您的。*不尊重XML嵌套。也就是说,你有
<row>
...
<row>
...
</row>
<row>
...
</row>
</row>
外部循环只拾取第一个内部行元素末尾的所有内容,并对其进行操作
教训?不要使用正则表达式进行XML解析。它可以用Perl之类的语言和扩展的RE语法来完成,但很快就会变得非常混乱。最好使用适当的XML库。您的。*不尊重XML嵌套。也就是说,你有
<row>
...
<row>
...
</row>
<row>
...
</row>
</row>
外部循环只拾取第一个内部行元素末尾的所有内容,并对其进行操作
教训?不要使用正则表达式进行XML解析。它可以用Perl之类的语言和扩展的RE语法来完成,但很快就会变得非常混乱。最好使用适当的XML库。请不要使用正则表达式解析XML数据。这是一种痛苦 也就是说,您的xml数据格式不好,因为您有几个根标记。我添加了一些内容以使其结构合理 这里有一个XML::Twig的示例: 像这样运行:
perl script.pl xmlfile
这将产生:
<root>
<tbl ID="I78">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody valign="top">
<row>
<entry colname="4">i.</entry>
<entry colname="4">181.10</entry>
<entry colname="4">An inmate shall comply with the dispositions imposed by a hearing officer in a Tier I, Tier II and Tier III hearings.</entry>
<entry colname="4">I, II, III</entry>
</row>
</tbody>
</tgroup>
</table>
</tbl>
<tbl ID="I93">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody>
<row>
<entry align="center" colname="4">
<ital>Pledge number</ital>
</entry>
<entry align="center" colname="4">
<ital>Date</ital>
</entry>
<entry align="center" colname="4">
<ital>R</ital>
</entry>
<entry colname="4">
<ital>A or S</ital>
</entry>
</row>
<row>
<entry colname="4">
<ital>Disposition column</ital>
</entry>
<entry colname="4">(<ital>Renewed</ital>)</entry>
<entry colname="4">(<ital>Renewed</ital>)</entry>
</row>
<row>
<entry colname="4">(<ital>Auction Sale</ital>)</entry>
</row>
</tbody>
</tgroup>
<eos></eos>
</table>
<eop></eop>
</tbl>
</root>
请不要使用正则表达式来解析xml数据。这是一种痛苦 也就是说,您的xml数据格式不好,因为您有几个根标记。我添加了一些内容以使其结构合理 这里有一个XML::Twig的示例: 像这样运行:
perl script.pl xmlfile
这将产生:
<root>
<tbl ID="I78">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody valign="top">
<row>
<entry colname="4">i.</entry>
<entry colname="4">181.10</entry>
<entry colname="4">An inmate shall comply with the dispositions imposed by a hearing officer in a Tier I, Tier II and Tier III hearings.</entry>
<entry colname="4">I, II, III</entry>
</row>
</tbody>
</tgroup>
</table>
</tbl>
<tbl ID="I93">
<table colsep="0" frame="none" rowsep="0">
<tgroup cols="4">
<tbody>
<row>
<entry align="center" colname="4">
<ital>Pledge number</ital>
</entry>
<entry align="center" colname="4">
<ital>Date</ital>
</entry>
<entry align="center" colname="4">
<ital>R</ital>
</entry>
<entry colname="4">
<ital>A or S</ital>
</entry>
</row>
<row>
<entry colname="4">
<ital>Disposition column</ital>
</entry>
<entry colname="4">(<ital>Renewed</ital>)</entry>
<entry colname="4">(<ital>Renewed</ital>)</entry>
</row>
<row>
<entry colname="4">(<ital>Auction Sale</ital>)</entry>
</row>
</tbody>
</tgroup>
<eos></eos>
</table>
<eop></eop>
</tbl>
</root>
我对perl非常陌生,第一次研究xml嵌套。谢谢你的建议,我会记下来的。。。。如果你看到正则表达式解析,别忘了尖叫我已经给了你。*?这应该是我第一次偶然发现的,而且我在xml数据中没有嵌套行。没错——不过,屏幕截图中的缩进看起来像是你做的。我对perl非常陌生,第一次研究xml嵌套。谢谢你的建议,我会记下来的。。。。如果你看到正则表达式解析,别忘了尖叫我已经给了你。*?首先,我会偶然发现,xml数据中没有嵌套行。是的–屏幕截图中的缩进看起来像是这样。运行此脚本是否会覆盖源文件?我只看到一个参数被传递给这个..@RaviKumar:不,它将把它打印到shell中。将其重定向到一个文件,如:perl script.pl xmlfile>out\u xmlfileok。在每一行中,如果有四个条目标记,colname值应该从1增加到4。您的输出显示常量值。我如何更改它?@RaviKumar:这是原始问题中的要求还是新的要求?当我第一次看到这个问题时,它就在那里。运行这个脚本会覆盖源文件吗?我只看到一个参数被传递给这个..@RaviKumar:不,它将把它打印到shell中。将其重定向到一个文件,如:perl script.pl xmlfile>out\u xmlfileok。在每一行中,如果有四个条目标记,colname值应该从1增加到4。您的输出显示常量值。我该如何改变它?@RaviKumar:这是原始问题中的要求还是新的要求?当我第一次看到这个问题时,它就在那里。