2012-01-13 109 views
0

嗨得到執行,我基於風格的空調當別人塊第二次

E.g

<list-bull> 
<P_list-bull>Use Microsoft Windows accessibility </P_list-bull> 
<P_list-bull> Magnify the display </P_list-bull> 
<P_list-simple> Change the size of text and icons </P_list-simple> 
<P_list-simple> Convert the text displayed </P_list-simple> 
<P_list-num> Change the contrast </P_list-num> 
<P_list-simple>text and icons </P_list-simple> 
<P_list-bull>Microsoft accessibility </P_list-bull> 
</list-bull> 

在上面的例子中是我輸入我生成標籤內的數字。我想根據樣式(bull,simple,num)生成數字,在第一次出現(任何類型)時,我要在標籤內部生成一個(1),然後在第二個樣式中生成2個,等等....

我想在下面的格式

<list-bull> 
<P_list-bull(1)>Use Microsoft Windows accessibility </P_list-bull(1)> 
<P_list-bull(1)> Magnify the display </P_list-bull(1)> 
<P_list-simple(2)> Change the size of text and icons </P_list-simple(2)> 
<P_list-simple(2)> Convert the text displayed </P_list-simple(2)> 
<P_list-num(3)> Change the contrast </P_list-num(3)> 
<P_list-simple(2)>text and icons </P_list-simple(2)> 
<P_list-bull(1)>Microsoft accessibility </P_list-bull(1)> 
</list-bull> 

輸出我嘗試這下面的代碼,但我D'知道在哪裏可以修復的情況

while($str =~ /<list-(bull|num|alpha|roman|simple)(?:(?:(?!<\/list-\1>).)*)<\/list-\1>/sgi){ 
$str =~ s#<list-(bull|num|alpha|roman|simple)(?:(?:(?!<\/list-\1>).)*)<\/list-\1>#&List_find($&,$1)#sgei; 
    } 
sub List_find 
{ 
my ($line,$type) = @_; 
my $currentlevel = 0; 
my $line1; 
while($line =~ s/<P_list-(bull|num|alpha|roman|simple)(?:(?:(?!<\/P_list-\1>).)*) <\/P_list-\1>/&Listnum($&)/sgie){} 



sub Listnum 
{ 
my $line2 = @_[0]; 

my ($style,$cont); 
if($line2 =~ /<P_list-(.*?)>(.*?)<\/P_list-\1>/sgi) 
{ 
$style = $1; 
$cont = $2; 
my $id =1; 
if($type eq $style){ 
$line2 = "<P_list-$style($id)>$cont</P_list-$style($id)>"; 

} 
else{ 

    $line2 =~ /<P_list-(.*?)>(.*?)<\/P_list-\1>/s; 
    my $temp = $1; 

    if($style eq $temp) 
     { 

    $id++; 
    $line2 = "<P_list-$style($id)>$cont</P_list-$style($id)>"; 
     } 
    else 
     { 
    $id = $id+2;   

    $line2 = "<P_list-$style($id)>$cont</P_list-$style($id)>";   
     } 
    } 
    return "$line2"; 
} 
} 

我第一個列表項標籤與第一比較列表類型(公牛),如果它是正確的,它會產生一個否則兩個,其他部分我想檢查第二和第四稅務局型

PLZ給一些建議

+3

爲什麼不使用正確的XML解析器? – Geo 2012-01-13 11:19:49

+2

...並讓它生成有效的XML ... – pavel 2012-01-13 11:35:25

+0

是否有可能在沒有XML解析器的情況下生成數字? – kula 2012-01-13 11:58:55

回答

1

不知道爲什麼不能使用一些XML解析器,但不管怎麼說..

得到的所有樣式的哈希鍵,這樣的事情:

my %styles=(); 
my $i=1; 
while($text=~/P_list-([a-zA-Z]+)/gsm) { 
    $styles{$1}=$i++ if !$styles{$1}; 
} 

則剛剛替補他們都

foreach my $k (keys %styles) { 
    my $what_you_have="P_list-$k"; 
    my $what_you_want="P_list-$k($styles{$k})"; 
    $text=~s/$what_you_have/$what_you_want/gsm; 
} 

假設$text包含您的全部輸入

相關問題