2015-10-19 26 views
0

我需要匹配rid="Vajravelu:1978"matchid="Vajravelu:1978"這兩個相同的id。如何匹配兩個相同的id並導入所需的id

Perl程序下面工作提到邏輯

步驟1 首先mathcing兩個相同的id rid="Vajravelu:1978"matchid="Vajravelu:1978"

步驟2 如果兩個ID matchid =「Vajravelu相同:1978對應參考編號等id="j_zna-2014-0260_ref_001"要被存儲在一個變量。

步驟3 此存儲的參考ID varible應寫入在RID = 'j_zna-2014-0260_ref_001'

步驟4 最後比賽ID應該被刪除

我輸入HTML文件:

<p>The sample ID [[<xref ref-type="bibr" rid="Vajravelu:1978">1</xref>, <xref ref-type="bibr" rid="Adesanya:2014">2</xref>]]</p> 

    <ref id="j_zna-2014-0260_ref_001"><label><matchid="Vajravelu:1978">[1]</label><mixed-citation><fnm>K.</fnm> <snm>Vajravelu</snm> and <fnm>K. S.</fnm> Sastri, J. Fluid Mech. <volume><bold>86</bold></volume>, 365 (1978).</mixed-citation></ref> 
    <ref id="j_zna-2014-0260_ref_002"><label><matchid="Narain:1982">[2]</label><mixed-citation>A. Narain and D. D. Joseph, Rheol. Acta. <bold>21</bold>, 228 (1982).</mixed-citation></ref> 

我需要的輸出XML文件:

<p>The sample ID [[<xref ref-type="bibr" rid="j_zna-2014-0260_ref_001">1</xref>, <xref ref-type="bibr" rid="j_zna-2014-0260_ref_002">2</xref>]]</p> 

<ref id="j_zna-2014-0260_ref_001"><label>[1]</label><mixed-citation><fnm>K.</fnm> <snm>Vajravelu</snm> and <fnm>K. S.</fnm> Sastri, J. Fluid Mech. <volume><bold>86</bold></volume>, 365 (1978).</mixed-citation></ref> 
<ref id="j_zna-2014-0260_ref_002"><label>[2]</label><mixed-citation>A. Narain and D. D. Joseph, Rheol. Acta. <bold>21</bold>, 228 (1982).</mixed-citation></ref> 

我的perl的編程初學者。我會嘗試下面提到的代碼。但是這個代碼不起作用。請指點

#!C:\Strawberry\perl\bin\perl 
use strict; 
use warnings; 
open(IN,"$ARGV[0]\.html")||die("Input LaTeX filename without .html extension\n"); 
local $/; 
my $TeX2html=<IN>; 
my ($rid) = $TeX2html =~ m/rid="(.*?)"/; 
my ($matchid) = $TeX2html =~ m/matchid="(.*?)"/; 

while($rid = $matchid) { 
         $TeX2html =~ s/ref id="(.*?)/rid=$1/si; 
         } 
print $TeX2html; 
close(IN); 

我使用命令來執行上述程序stack.pl sample >out.xml

+1

首先'嚴格使用''使用警告;'應該是你的程序的第一行你的「shebang」行是錯誤的 - 但這並不是' (你應該提供解釋器的路徑,例如'C:\ Strawberry \ perl \ bin \ perl') – Sobrique

回答

4
  • use strict;use warnings;應該是你的程序的第一線。

  • 你的「shebang」行是錯誤的 - 但這並不重要,因爲它是Windows。 (你應該提供翻譯的路徑,例如C:\Strawberry\perl\bin\perl

  • 你爲什麼設置$/chr(26)?如果您正在閱讀整個文件,請使用File::Slurplocal $/;,將其設置爲undef

  • 這並不做任何事情:

    $rid = rid="(.*?)"; 
    $matchid = matchid="(.*?)"; 
    

strictwarnings會告訴你這一點。

如果你想正則表達式匹配你的內容,你需要m/$regex/如果你與$_工作(在while循環)或指定你在操作什麼:

my ($rid) = $TeX2html =~ m/rid="(.*?)"/; 
my ($matchid) = $TeX2html =~ m/matchid="(.*?)"/; 
  • while($rid = $matchid) {不是條件性的,它是一項任務。您的數值可能是==,而基於字符串的eq。結果,它將會無限循環。(strictwarnings會告訴你這個太)

  • if($rid =~ s/ref id="(.*?)"/rid=$1/ig) { - 是無稽之談,因爲$rid完全是另一回事。你在第一個正則表達式中捕獲文本Vajravelu:1978(在兩者中)。所以你的s搜索和替換模式...是不會匹配什麼在左邊,所以永遠不會取代任何東西,所以會無限循環。 (即使有條件的while正確的排序)。

也許你的意思改變$TeX2html而非$rid? (不能完全肯定,但因爲現在我就帶你想做什麼失去

編輯:我到目前爲止參考什麼:

#!c:\Strawberry\perl\bin 
use strict; 
use warnings; 

local $/; 
my $TeX2html=<DATA>; 

my ($rid) = $TeX2html =~ m/rid="(.*?)"/; 
my ($matchid) = $TeX2html =~ m/matchid="(.*?)"/; 

print $rid; 
print $matchid; 

#sorry - no idea what this is trying to do. 
#if ($rid eq $matchid) { 
#  if($rid =~ s/ref id="(.*?)"/rid=$1/ig) { 
#     print "$rid"; 
#  } 
# } 

print $TeX2html; 


__DATA__ 
<p>The sample ID [[<xref ref-type="bibr" rid="Vajravelu:1978">1</xref>, <xref ref-type="bibr" rid="Adesanya:2014">2</xref>]]</p> 

    <ref id="j_zna-2014-0260_ref_001"><label><matchid="Vajravelu:1978">[1]</label><mixed-citation><fnm>K.</fnm> <snm>Vajravelu</snm> and <fnm>K. S.</fnm> Sastri, J. Fluid Mech. <volume><bold>86</bold></volume>, 365 (1978).</mixed-citation></ref> 
    <ref id="j_zna-2014-0260_ref_002"><label><matchid="Narain:1982">[2]</label><mixed-citation>A. Narain and D. D. Joseph, Rheol. Acta. <bold>21</bold>, 228 (1982).</mixed-citation></ref> 

不過說真的 - 我不知道理解while循環邏輯,所以我不能真正幫助

+0

我正在使用perl程序在後期處理中清理文件階段。我通過perl程序獲取輸出html到xml。我的輸入文件不是唯一的ID。但是xml輸出想要保持唯一的id。我的要求是ref id =「」應該被替換爲rid =「」。我將嘗試匹配文本和引用部分$ rid和$ matchid變量。這兩個ID匹配$ rid,$ matchid對應的ref id =「」應替換爲rid =「」。我不知道如何得到這個輸出 – Vetri

+0

你用什麼來做XML? – Sobrique

+0

我遵循你的指令,但是如何執行while循環。該程序未編譯。我編輯並保存我的問題 – Vetri

相關問題