2016-01-19 32 views
1

新的perl編碼器在這裏。問題Perl正則表達式

當我從網站複製並粘貼文本到文本文件並從該文件讀取時,我的perl腳本沒有問題。當我使用getstore從網站自動創建一個文件時,這是我想要的,輸出是一堆|的。

當我複製並粘貼或使用getstore下載文本時,文本看起來完全相同..我無法弄清楚問題所在。任何幫助將不勝感激。

,我的願望是如下的輸出:

|www\.arkinsoftware\.in|www\.askmeaboutrotary\.com|www\.assculturaleincontri\.it|www\.asu\.msmu\.ru|www\.atousoft\.com|www\.aucoeurdelanature\. 
enter code here 

這裏是我使用的代碼:

#!/usr/bin/perl 

use strict; 
use warnings; 
use LWP::Simple; 

getstore("http://www.malwaredomainlist.com/hostslist/hosts.txt",  "malhosts.txt"); 

open(my $input, "<", "malhosts.txt"); 

while (my $line = <$input>) { 
    chomp $line; 
    $line =~ s/.*\s+//; 
    $line =~ s/\./\\\./g; 
    print "$line\|"; 
} 

回答

0

一堆|你得到的,是從一開始就不合格的評論。所以解決方案是忽略所有「不合格」的行。

所以不是

$line =~ s/.*\s+//; 

使用

next unless $line =~ s/^127.*\s+//; 

所以你會忽略除茨艾倫每行開頭127

0

這裏就是我想要做的:

my $first = 1; 
while (<$input>) { 
    /^127\.0\.0\.1\s+(.+?)\s*$/ or next; 
    print '|' if !$first; 
    $first = 0; 
    print quotemeta($1); 
} 

這以更精確的方式匹配您的輸入,quotemeta負責真正的正則表達式轉義。

0

我可能會喜歡的東西去:

#!/usr/bin/perl 

use strict; 
use warnings; 
use LWP::Simple; 

getstore("http://www.malwaredomainlist.com/hostslist/hosts.txt", 
    "malhosts.txt"); 

open(my $input, "<", "malhosts.txt"); 

print join ("|", 
     map { m/^\d/ && ! m/localhost/ ? 
       quotemeta ((split)[1]) :() } <$input>); 

給:

0koryu0\.easter\.ne\.jp|1\-atraffickim\.tf|10\-trafficimj\.tf|109\-204\-26\-16\.netconnexion\.managedbroadband\.co\.uk|11\-atraasikim\.tf|11\.lamarianella\.info|12\-tgaffickvcmb\.tf| #etc.