Perl pattern extraction

Discussion in 'Perl Misc' started by Deepan - M.Sc(SE) - 03MW06, Mar 8, 2008.

  1. Hi,

    my $url = "/pages-cell.net/deepan/sony/";

    if($url =~ m/\/(.*)\//g)
    {
    my @result = $1;
    return @result;
    }

    What i need is that i should be able to get anything that is between /
    and /. Here i should be able to get pages-cell.net,deepan,sony into
    @result but something is wrong somewhere. Please help me to solve
    this?

    Thanks,
    Deepan
     
    Deepan - M.Sc(SE) - 03MW06, Mar 8, 2008
    #1
    1. Advertising

  2. Deepan - M.Sc(SE) - 03MW06

    Ben Morrow Guest

    Quoth "Deepan - M.Sc(SE) - 03MW06" <>:
    > Hi,
    >
    > my $url = "/pages-cell.net/deepan/sony/";
    >
    > if($url =~ m/\/(.*)\//g)
    > {
    > my @result = $1;
    > return @result;
    > }
    >
    > What i need is that i should be able to get anything that is between /
    > and /. Here i should be able to get pages-cell.net,deepan,sony into
    > @result but something is wrong somewhere. Please help me to solve
    > this?


    * is greedy by default, meaning it matches as much as possible. So you
    either make it not greedy

    m!/(.*?)/!g

    or you are more specific about what can match

    m!/([^/]*)/!g

    .. Note that m//g in an 'if' clause will only return the first match,
    making the /g somewhat pointless.

    If you were thinking that

    my @result = $1;

    would assign *all* the matches to @result, then you have misunderstood
    how scalar variables work in Perl. $1 can only ever contain one value.
    If you want to split $url on slashes, you can just use split:

    my @result = split '/', $url;
    shift @result;

    Ben


    >
    > Thanks,
    > Deepan
     
    Ben Morrow, Mar 8, 2008
    #2
    1. Advertising

  3. Deepan - M.Sc(SE) - 03MW06 wrote:
    >
    > my $url = "/pages-cell.net/deepan/sony/";
    >
    > if($url =~ m/\/(.*)\//g)
    > {
    > my @result = $1;
    > return @result;
    > }
    >
    > What i need is that i should be able to get anything that is between /
    > and /. Here i should be able to get pages-cell.net,deepan,sony into
    > @result but something is wrong somewhere. Please help me to solve
    > this?


    my @result = $url =~ m#/(.*?)(?=/)#g;

    Better yet:

    my @result = split /\//, $url;

    perldoc -f split

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 8, 2008
    #3
  4. Deepan - M.Sc(SE) - 03MW06 wrote:
    > Hi,
    >
    > my $url = "/pages-cell.net/deepan/sony/";
    >
    > if($url =~ m/\/(.*)\//g)
    > {
    > my @result = $1;
    > return @result;
    > }
    >
    > What i need is that i should be able to get anything that is between /
    > and /. Here i should be able to get pages-cell.net,deepan,sony into
    > @result but something is wrong somewhere. Please help me to solve
    > this?


    my @result = $url =~ /[^\/]+/g



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Mar 8, 2008
    #4
  5. Chris Mattern <> wrote:
    > On 2008-03-08, Deepan - M.Sc(SE) - 03MW06 <> wrote:
    >> Hi,
    >>
    >> my $url = "/pages-cell.net/deepan/sony/";
    >>
    >> if($url =~ m/\/(.*)\//g)
    >> {
    >> my @result = $1;
    >> return @result;
    >> }
    >>
    >> What i need is that i should be able to get anything that is between /
    >> and /. Here i should be able to get pages-cell.net,deepan,sony into
    >> @result but something is wrong somewhere. Please help me to solve
    >> this?
    >>

    > No. m\/(.*)\//g only returns one string; m *always* only returns one
    > string.



    No it doesn't.

    The m// operator always returns one string when in scalar context.

    (in fact, *every* operator can return only one thing in scalar context.)

    m// in list context can potentially evaluate to more than one string.

    ---------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $_ = 'foo bar';

    my @matches = m/(\w+)\s+(\w+)/;

    foreach (@matches) {
    print "$_\n";
    }
    ---------------


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 9, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ram Laxman
    Replies:
    1
    Views:
    427
    Brad Baxter
    Jul 15, 2004
  2. Peekachu
    Replies:
    1
    Views:
    339
    Oliver Wong
    Jul 10, 2006
  3. aekalman
    Replies:
    6
    Views:
    139
    Ben Morrow
    Nov 22, 2004
  4. doni

    Pattern Matching and Extraction

    doni, Jan 22, 2007, in forum: Perl Misc
    Replies:
    10
    Views:
    182
    John Bokma
    Jan 23, 2007
  5. Deepan - M.Sc(SE) - 03MW06

    Perl pattern matching and extraction

    Deepan - M.Sc(SE) - 03MW06, Mar 8, 2008, in forum: Perl Misc
    Replies:
    1
    Views:
    114
    Gunnar Hjalmarsson
    Mar 8, 2008
Loading...

Share This Page