zer has asked for the wisdom of the Perl Monks concerning the following question:

i am looking to parse through some urls to basicly eliminate the ones that take me off of site. I appologise for the messyness but here is the code, and ill explain a bit for everyone.
Code
@list = grep (m/(?($_=~ m;^http;)localhost.net|^\/)/;
Test Data
/sered/uplink <--test true
/my/projects/ <--test true
heh/reg <--test false
http://www.OtherNetwork.org/network/ <--test false
http://www.localhost.net/hello <--test true

Explained
ok so with this regex i want to test for the same scalar that the grep would currently be testing. Basicly saying, if it starts with an "http" it better be on this server. However if it doesnt start with that it has to start with a "/" for a sub directory.

Problem
I am unable to get this to work properly

Appologies
I am new to this advanced regex format so please be patient with me

Replies are listed 'Best First'.
Re: Conditional Regex
by Sidhekin (Priest) on Nov 27, 2006 at 06:41 UTC

    If I read you right, you want to test that the string starts with either a "/" or "http://localhost.net" (with variations). On that assumption, an alternation (plus some variation) should do it (/x whitespace added for clarity):

    m,^(?: / | http:// [^/]* \b localhost\.net ),x

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Conditional Regex
by fenLisesi (Priest) on Nov 27, 2006 at 12:09 UTC
Re: Conditional Regex
by Firefly258 (Beadle) on Nov 27, 2006 at 16:27 UTC
    It seems quite simple, if I understand you right, you want to filter out values that don't begin with http://www.localhost.net or /.

    Maybe this is what you need?

    #!/usr/bin/perl -Wl use strict; my $my_domain = quotemeta 'http://www.localhost.net'; print for grep /^(?:$my_domain|\/)/, qw[ /sered/uplink /my/projects/ heh/reg http://www.OtherNetw +ork.org/network/ http://www.localhost.net/hello ];


    perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er