Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

regexp to return rest of string

by princepawn (Parson)
on Jun 03, 2000 at 00:44 UTC ( [id://16117]=perlquestion: print w/replies, xml ) Need Help??

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

I have this string:
"/home/angryman/www/content/m/i/2000/5/31/r_standoff.gif"
I want everything after content to be bound to a string. Ie, in this case I get
$relative_url="/m/i/2000/5/31/r_standoff.gif";

Replies are listed 'Best First'.
Re: regexp to return rest of string
by plaid (Chaplain) on Jun 03, 2000 at 00:48 UTC
    $string = "/home/angryman/www/content/m/i/2000/5/31/r_standoff.gif"; $string =~ /content(.*)/; $relative_url = $1;
RE: regexp to return rest of string
by byoung (Initiate) on Jun 03, 2000 at 04:22 UTC
    I've always been partial to the method in the magenta Camel:
    $dir = "/home/angryman/www/content/m/i/2000/5/31/r_standoff.gif"; ($dir =~ /content(.*)/) && ($after = $1);
    It is, of course especially clean when you are looping:
    while (<>) { (/foo(.*)/) && ($foo = $1); }
      oh yeah, nice one.
      definetly my favorite here...
RE: regexp to return rest of string
by neshura (Chaplain) on Jun 03, 2000 at 00:49 UTC
    Have you already tried to write a regex yourself? Perhaps you could show us the code and we could help you figure out where it goes wrong. My $0.02 anyway...

    e-mail neshura

Re: regexp to return rest of string
by cwest (Friar) on Jun 03, 2000 at 00:51 UTC
    my $path = q(/home/angryman/www/content/m/i/2000/5/31/r_standoff.gif); my $base = q(/home/angryman/www/content); ( my $rel_path = $path ) =~ s/^$base//;
    --
    Casey
    
Re: regexp to return rest of string
by princepawn (Parson) on Jun 03, 2000 at 00:50 UTC
    Or in one line:
    $relative_url = ($string =~ /content(.*)/, $1)[1];
    Thanks to Joe Hall's Effective Perl Programming. Thanks plaid.
      Humm... I seriously don't get it. And I don't mean to sound rude, but you're saying that you can reduce the two regex lines to one regex/ary line, but you can't figure out the regex itself? I'd have more trouble getting through the reduction than doing that simple regex. It just sounds fishy.

      Is this a case of knowing a previous language before perl, or something of the sort?

      That was a cool trick, btw.

      #!/home/bbq/bin/perl
      # Trust no1!
        I figured out this trick (or actually a variant) just by lots of trial and error with regexs in my early perl days. I prefer using parentheses rather than array subscripts, eg:

        ($relative_url) = ($string =~ /content(.*)/);

        A _very_ useful version of this trick is with DBI, ie:

        $getStuff = $dbh->prepare("select id, price, info from mytable");
        $getStuff->execute();
        ($id, $price, $info) = $getStuff->fetchrow_array();
        I use this a ton.

        -- Kirby

        Tuxtops: Laptops with Linux!

Re: regexp to return rest of string
by KM (Priest) on Jun 03, 2000 at 00:50 UTC
    Read perlre, learn how to do this, it could save your life. Ok, well it may not save your life but you will certainly benefit from it. If you have read it, reread it. Also, please post what you have already tried, so we may see what is going wrong.

    Cheers,
    KM

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found