Pattern extraction

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

  1. Sorry i didnt explain the previous posy correctly.

    I am having a string which can have either one of the below as its
    input:

    $str="/a/b/c/"; (or) $str = "/a/b/c/d/";


    What i need is that regardless of the input it should always be able
    to extract "c" from the
    above strings. I should not use split. Only by using regular
    expressions i should be able to achieve this. Please help me to solve
    this.

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

  2. [ You keep starting new threads for the same problem. Don't do that!! ]

    Deepan - M.Sc(SE) - 03MW06 wrote:
    > I am having a string which can have either one of the below as its
    > input:
    >
    > $str="/a/b/c/"; (or) $str = "/a/b/c/d/";
    >
    > What i need is that regardless of the input it should always be able
    > to extract "c" from the
    > above strings. I should not use split.


    Says who?

    > Only by using regular expressions i should be able to achieve this.


    split() does make use of a regular expression...

    Anyway,

    if ( $str =~ m#^/[^/]+/[^/]+/([^/]+)# ) {
    print "$1\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 10, 2008
    #2
    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:
    420
    Brad Baxter
    Jul 15, 2004
  2. Peekachu
    Replies:
    1
    Views:
    321
    Oliver Wong
    Jul 10, 2006
  3. aekalman
    Replies:
    6
    Views:
    121
    Ben Morrow
    Nov 22, 2004
  4. doni

    Pattern Matching and Extraction

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

    Perl pattern extraction

    Deepan - M.Sc(SE) - 03MW06, Mar 8, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    87
    Tad J McClellan
    Mar 9, 2008
Loading...

Share This Page