🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.perl
Article #52067

regexp do wycinania parametrów z urla

#52067
From: Krzysztof Krzyżaniak
Date: Thu, 02 Dec 2010 15:08
70 lines
2974 bytes
Potrzebowałbym znaleźć regexp który mi wytnie jeden parametr z urla i
zrobi url bez tego parametru, konkretne to wycięcie image=cos lub image=

czyli np. 

/index.php?test=1&image=cos&p=2 => /index.php?test=1&p=2
/index.php?test=1&image=cos&p=2&r=4 => /index.php?test=1&p=2&r=4
/index.php?b=2&a=1&image=cos&p=2&r=4 => /index.php?b=2&a=1&p=2&r=4
/index.php?image=cos&p=3 => /index.php?p=3
/index.php?image=&p=3 => /index.php?p=3
/index.php?p2=2&image=cos => /index.php?p2=2
/index.php?title=image&sometest=1 => /index.php?title=image&sometest=1
/index.php?title=image&someimage=data&test=1 => /index.php?title=image&sometest=1
/index.php?title=image&someimage=data&test=1 => /index.php?title=image&someimage=data&test=1

#!/usr/bin/perl

use common::sense;

my @b = (
   "/index.php?test=1&image=cos&p=2",
   "/index.php?test=1&image=cos&p=2&r=4",
   "/index.php?b=2&a=1&image=cos&p=2&r=4",
   "/index.php?image=cos&p=3",
   "/index.php?image=&p=3",
   "/index.php?p2=2&image=cos",
   "/index.php?title=image&sometest=1",
   "/index.php?title=image&someimage=data&test=1"
);

my @a = (
   "/index.php?test=1&p=2",
   "/index.php?test=1&p=2&r=4",
   "/index.php?b=2&a=1&p=2&r=4",
   "/index.php?p=3",
   "/index.php?p=3",
   "/index.php?p2=2",
   "/index.php?title=image&sometest=1",
   "/index.php?title=image&someimage=data&test=1"
);

my $r = '([^&]*)image=[^&]*&*(.*)';
my $i = 0;
for my $t ( @b ) {
    print $t;
    $t =~ s/$r/\1\2/;
    if ( $t eq  $a[ $i ] ) {
       say " => $t OK";
    }
    else {
       say " => $t <> $a[ $i ] ERROR";
    }
    $i++;
}

wysypuje się na dwóch przykładach:

/index.php?p2=2&image=cos => /index.php?p2=2& <> /index.php?p2=2 ERROR
/index.php?title=image&someimage=data&test=1 => /index.php?title=image&sometest=1 <> /index.php?title=image&someimage=data&test=1 ERROR


i w sumie po pół godzinie nie jestem już pewien czy da się to zrobić
regexpem. Jakieś szybkie pomysły?

  eloy
-- 
-------e-l-o-y----------------------------e-l-o-y-@-k-o-f-e-i-n-a-.-n-e-t------

       jak to dobrze, że są oceany - bez nich byłoby jeszcze smutniej
Technical Details
Message-ID: <gnus.87k4js43h1.fsf.eloy@kofeina.net>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail
From: Krzysztof Krzyżaniak <eloy@kofeina.net>
Newsgroups: pl.comp.lang.perl
Subject: regexp do wycinania parametrów z urla
Date: Thu, 02 Dec 2010 16:08:26 +0100
Organization: http://www.wikia.com/ -- We are good guys!
Lines: 70
Message-ID: <gnus.87k4js43h1.fsf.eloy@kofeina.net>
NNTP-Posting-Host: chlor.kofeina.net
Mime-Version: 1.0
X-Trace: inews.gazeta.pl 1291302613 22010 194.114.145.51 (2 Dec 2010 15:10:13 GMT)
X-Complaints-To: usenet@agora.pl
NNTP-Posting-Date: Thu, 2 Dec 2010 15:10:13 +0000 (UTC)
Cancel-Lock: sha1:WF7ZPab5d2Da7Efv+sggiXhjTQ4=
X-User: eloy
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit