Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Conditional Regex

by zer (Deacon)
on Nov 27, 2006 at 06:22 UTC ( [id://586189]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://586189]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found