how to parse string from [?

Discussion in 'Perl Misc' started by sam, Apr 9, 2004.

  1. sam

    sam Guest

    Hi,

    I have the following string need to be parsed in perl,
    "sshd(pam_unix)[2009]"

    I want to extract the first the word up to "[", what is the regular
    expression can do that?
    The following regular express does not work:
    "m/^([^\[]*)\[.*]$"

    Thanks
    sam
     
    sam, Apr 9, 2004
    #1
    1. Advertising

  2. sam wrote:
    > I have the following string need to be parsed in perl,
    > "sshd(pam_unix)[2009]"
    >
    > I want to extract the first the word up to "[", what is the regular
    > expression can do that?
    > The following regular express does not work:
    > "m/^([^\[]*)\[.*]$"


    No? Exactly what did you expect it to do, and what did it do? Please
    post complete code.

    (I take for granted that the missing trailing slash is a typo when you
    retyped the code into the message. Don't retype - copy and paste!)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 9, 2004
    #2
    1. Advertising

  3. sam

    sam Guest

    Jim Gibson wrote:

    > On 2004-04-09 09:51:10 -0700, sam <> said:
    >
    >> Hi,
    >>
    >> I have the following string need to be parsed in perl,
    >> "sshd(pam_unix)[2009]"
    >>
    >> I want to extract the first the word up to "[", what is the regular
    >> expression can do that?
    >> The following regular express does not work:
    >> "m/^([^\[]*)\[.*]$"
    >>
    >> Thanks
    >> sam

    >
    >
    > "does not work" is a little vague. You need to terminate the regular
    > expression with another '/'. Except for that, your expression works for
    > me. You can simplify it a little by 1) not escaping ']' in the character
    > class (it doesn't need it), and 2) not including the extraneous '\[.*]$'
    > at the end, unless you are trying to eliminate lines that do not match
    > that part.
    >
    > Please post a complete, working program and tell us what it is doing
    > that "does not work".
    >


    Thanks for your suggestion, here is my simplified perl script:
    #!/usr/bin/perl -w
    use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
    sshd(pam_unix)[2009]

    Note, there is no "\n" at the end of the "sshd(..)[2009]" string.

    thanks
    sam
     
    sam, Apr 9, 2004
    #3
  4. sam

    Paul Lalli Guest

    On Sat, 10 Apr 2004, sam wrote:

    > Jim Gibson wrote:
    >
    > Thanks for your suggestion, here is my simplified perl script:
    > #!/usr/bin/perl -w
    > use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
    > sshd(pam_unix)[2009]
    >
    > Note, there is no "\n" at the end of the "sshd(..)[2009]" string.
    >


    Why are you putting quotes around your whole regexp? You're suddenly
    asking if this big long string returns a true value, rather than whether
    or not a pattern match succeeds. Get rid of those quotes.

    print "$1\n" if /^([^[]*)/;


    Paul Lalli
     
    Paul Lalli, Apr 9, 2004
    #4
  5. sam

    sam Guest

    Paul Lalli wrote:

    > On Sat, 10 Apr 2004, sam wrote:
    >
    >
    >>Jim Gibson wrote:
    >>
    >>Thanks for your suggestion, here is my simplified perl script:
    >>#!/usr/bin/perl -w
    >>use strict; while (<DATA>) { print "$1\n" if "m/^([^\[]*)\[.*$/"; } __DATA__
    >>sshd(pam_unix)[2009]
    >>
    >>Note, there is no "\n" at the end of the "sshd(..)[2009]" string.
    >>

    >
    >
    > Why are you putting quotes around your whole regexp? You're suddenly
    > asking if this big long string returns a true value, rather than whether
    > or not a pattern match succeeds. Get rid of those quotes.
    >
    > print "$1\n" if /^([^[]*)/;
    >

    Thanks, it works now.
    sam
    >
    > Paul Lalli
     
    sam, Apr 9, 2004
    #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. Vincent

    parse string

    Vincent, Dec 16, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,818
    Gunnar Hjalmarsson
    Dec 16, 2003
  2. Kevin Joseph
    Replies:
    5
    Views:
    4,352
    Joe Smith
    Aug 13, 2004
  3. Henri
    Replies:
    2
    Views:
    322
    Henri
    Nov 29, 2004
  4. Replies:
    19
    Views:
    1,146
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    192
    7stud --
    Feb 20, 2008
Loading...

Share This Page