Pattern extraction

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

  1. I am having a string like below:

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

    What i need is that i 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. Deepan - M.Sc(SE) - 03MW06 <> wrote:
    > I am having a string like below:
    >
    > $str="/a/b/c/"; (or) $str = "/a/b/c/d/";
    >
    > What i need is that i should always be able to extract "c" from the
    > above strings. I should not use split.



    Why not?


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

  3. "Deepan - M.Sc(SE) - 03MW06" <> wrote:
    >I am having a string like below:
    >
    > $str="/a/b/c/"; (or) $str = "/a/b/c/d/";
    >
    >What i need is that i should always be able to extract "c" from the
    >above strings. I should not use split.


    Why? It is the most natural tool for this task:

    my $res = (split('/', $str))[3];

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


    1: the first argument of split() is a regular expressions
    2: As stated the task is impossible because you need some other function
    beside the RE to apply the RE, e.g. a s/// or m//

    jue
     
    Jürgen Exner, Mar 10, 2008
    #3
  4. >>>>> "D" == "Deepan <- M.Sc(SE) - 03MW06" <>> writes:

    D> I am having a string like below: $str="/a/b/c/"; (or) $str =
    D> "/a/b/c/d/";

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

    Split is the best way to solve this in Perl.

    If this restriction is because that's what your teacher said, go ask
    your teacher. He or she is being paid to teach you; we are not.

    If this restriction is because you're using some language other than
    Perl, which doesn't have a split function, ask in a forum appropriate
    for that language.

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Mar 10, 2008
    #4
  5. Deepan - M.Sc(SE) - 03MW06

    szr Guest

    Abigail wrote:
    > _
    > Deepan - M.Sc(SE) - 03MW06 () wrote on VCCCV
    > September
    > MCMXCIII in
    > <URL:news:>:
    > "" I am having a string like below: ""
    > "" $str="/a/b/c/"; (or) $str = "/a/b/c/d/";
    > ""
    > "" What i need is that i 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.
    >
    >
    > Easy piecy!
    >
    >
    > $str = "/a/b/c/"; # Or "/a/b/c/d/".
    > my $answer = substr $str, 5, 1;
    >
    > say $answer;
    >
    > #
    > # Dummy regexp to satisfy the homework requirements.
    > #
    > "" =~ /nananana/;



    "say" ?

    Can't call method "say" without a package or object reference


    I am curious where this "say" came from?


    $ perldoc say
    No documentation found for "say".

    $ perldoc -f say
    No documentation for perl function `say' found

    $ perldoc -q say
    No documentation for perl FAQ keyword `say' found


    Thanks.

    --
    szr
     
    szr, Mar 16, 2008
    #5
  6. szr wrote:
    > Abigail wrote:
    >> _
    >> Easy piecy!
    >>
    >> $str = "/a/b/c/"; # Or "/a/b/c/d/".
    >> my $answer = substr $str, 5, 1;
    >>
    >> say $answer;

    >
    > "say" ?
    >
    > Can't call method "say" without a package or object reference
    >
    >
    > I am curious where this "say" came from?
    >
    >
    > $ perldoc say
    > No documentation found for "say".
    >
    > $ perldoc -f say
    > No documentation for perl function `say' found
    >
    > $ perldoc -q say
    > No documentation for perl FAQ keyword `say' found


    say() is a new feature of Perl version 5.10 so you won't see it until
    you upgrade.


    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 16, 2008
    #6
  7. Deepan - M.Sc(SE) - 03MW06

    szr Guest

    John W. Krahn wrote:
    > szr wrote:
    >> Abigail wrote:
    >>> _
    >>> Easy piecy!
    >>>
    >>> $str = "/a/b/c/"; # Or "/a/b/c/d/".
    >>> my $answer = substr $str, 5, 1;
    >>>
    >>> say $answer;

    >>
    >> "say" ?
    >>
    >> Can't call method "say" without a package or object reference
    >>
    >>
    >> I am curious where this "say" came from?
    >>
    >>
    >> $ perldoc say
    >> No documentation found for "say".
    >>
    >> $ perldoc -f say
    >> No documentation for perl function `say' found
    >>
    >> $ perldoc -q say
    >> No documentation for perl FAQ keyword `say' found

    >
    > say() is a new feature of Perl version 5.10 so you won't see it until
    > you upgrade.


    Thanks. I had a feeling. I am currently building 5.10.0 right now as I
    type this :)


    --
    szr
     
    szr, Mar 16, 2008
    #7
    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:
    430
    Brad Baxter
    Jul 15, 2004
  2. Peekachu
    Replies:
    1
    Views:
    342
    Oliver Wong
    Jul 10, 2006
  3. aekalman
    Replies:
    6
    Views:
    147
    Ben Morrow
    Nov 22, 2004
  4. doni

    Pattern Matching and Extraction

    doni, Jan 22, 2007, in forum: Perl Misc
    Replies:
    10
    Views:
    185
    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:
    108
    Tad J McClellan
    Mar 9, 2008
Loading...

Share This Page