Why is this regex logic failing?

时间:2015-12-04 21:18:54

标签: regex perl

I'm trying to build a quick script that will take a url, and check it against a list of PCREs to see if there's a match. However, it doesn't seem to be working. I've tried printing everything to make sure it's output the way I want (including the ARGV[0], passing it with single quotes appears to keep all the characters in tact). But it's still not working.

This is the script

#!/usr/bin/perl                                                                                             

use strict;
use warnings;

if (not($ARGV[0])) {
    die "Useage: checkurl.pl \"<url>\"";
}

if ($ARGV[1]) {
    die "Too many command line arguments, try checkurl.pl \"<url>\"";
}

$_ = $ARGV[0];
print "$_\n";
my $file = "pcre.txt";

open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>) {
    if (/$line/) {
        print "Match found, the url matches the following PCRE: \n";
        print "$line\n";
    }
}

This is the test URL (warning, this was an actual Angler EK link, I've defanged it, just in case it's still live, so you have to fix it to properly check the PCRE)

hxxp://nosprivsliikeradan.pfgfoxriver-localguide2[.]com/boards/viewforum.php?f=5x827&sid=7q0as14.5i4x8

This is the PCRE in the pcre.txt file that matches the above URL

^http:\/\/(?!www|forums?)[^\.]+\.[^\.]+\.(?:[^\.\x2f]+?|[^\.]+\.[^\.]{2})\/[a-z]+\/?view(?:forum|topic)\.php\?[a-z]=(?=[^\n]{0,64}\.)[0-9a-z\.]{1,6}(?:&[a-z0-9]*=[0-9a-z\.]*){1,2}$

1 个答案:

答案 0 :(得分:3)

您的模式实际上是#!/usr/bin/perl use strict; use warnings; my $WIDTH = 80; if ($ARGV[0] =~ /^[1-9][0-9]*$/) { $WIDTH = $ARGV[0]; shift @ARGV; } while (<>) { s/\r\n$/\n/; chomp; if (length $_ <= $WIDTH) { print "$_\n"; next; } @_=split /(\s+)/; # make @_ start with a separator field and end with a content field unshift @_, ""; push @_, "" if @_%2; my ($sep,$cont) = splice(@_, 0, 2); do { if (length $cont > $WIDTH) { print "$cont"; ($sep,$cont) = splice(@_, 0, 2); } elsif (length($sep) + length($cont) > $WIDTH) { printf "%*s%s", $WIDTH - length $cont, "", $cont; ($sep,$cont) = splice(@_, 0, 2); } else { my $remain = $WIDTH; { do { print "$sep$cont"; $remain -= length $sep; $remain -= length $cont; ($sep,$cont) = splice(@_, 0, 2) or last; } while (length($sep) + length($cont) <= $remain); } } print "\n"; $sep = ""; } while ($cont); } ,因为您从文件中读取它并且它包含换行符。在将其插入匹配运算符之前,您需要chomp该行:

/^...$\n/
相关问题