Newbie: Simple conditional on regexp match

Discussion in 'Perl Misc' started by Kenneth Brun Nielsen, Aug 6, 2008.

  1. I need to perform a conditional on a regexp match. How can I do that
    (easily) in PERL.

    The following prints out all lines - also the ones, that doesnt match
    the regular expression.

    #!/usr/bin/perl -w
    open FILEHANDLE, "soatest.soa";
    while (<FILEHANDLE>){
    if (/^\*| XI/) {
    print "match in line: $.\n";
    }
    }
     
    Kenneth Brun Nielsen, Aug 6, 2008
    #1
    1. Advertising

  2. On Aug 6, 11:54 am, Kenneth Brun Nielsen
    <> wrote:
    > I need to perform a conditional on a regexp match. How can I do that
    > (easily) in PERL.
    >
    > The following prints out all lines - also the ones, that doesnt match
    > the regular expression.
    >
    > #!/usr/bin/perl -w
    > open FILEHANDLE, "soatest.soa";
    > while (<FILEHANDLE>){
    >     if (/^\*| XI/) {
    >         print "match in line: $.\n";
    >     }
    >
    >
    >
    > }


    DOH! I found out that the above works as intended. I just "forgot" to
    escape the | in the regexp.
     
    Kenneth Brun Nielsen, Aug 6, 2008
    #2
    1. Advertising

  3. Kenneth Brun Nielsen

    Guest

    Kenneth Brun Nielsen <> wrote:
    > I need to perform a conditional on a regexp match. How can I do that
    > (easily) in PERL.
    >
    > The following prints out all lines - also the ones, that doesnt match
    > the regular expression.


    I don't believe you. Show us an example of soatest.soa that demonstrates
    this behavior.

    >
    > #!/usr/bin/perl -w
    > open FILEHANDLE, "soatest.soa";
    > while (<FILEHANDLE>){
    > if (/^\*| XI/) {
    > print "match in line: $.\n";
    > }
    > }


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Aug 6, 2008
    #3
  4. Kenneth Brun Nielsen

    szr Guest

    wrote:
    > Kenneth Brun Nielsen <> wrote:
    >> I need to perform a conditional on a regexp match. How can I do that
    >> (easily) in PERL.
    >>
    >> The following prints out all lines - also the ones, that doesnt match
    >> the regular expression.

    >
    > I don't believe you. Show us an example of soatest.soa that
    > demonstrates this behavior.


    He said in his other reply that he forgot to escape the pipe in the
    regex (so it wasn't meant to be an alternation.)

    >> #!/usr/bin/perl -w
    >> open FILEHANDLE, "soatest.soa";
    >> while (<FILEHANDLE>){
    >> if (/^\*| XI/) {
    >> print "match in line: $.\n";
    >> }
    >> }


    So it should be:

    while (<FILEHANDLE>){
    if (/^\*\| XI/) {

    --
    szr
     
    szr, Aug 16, 2008
    #4
    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. Mikel Lindsaar
    Replies:
    0
    Views:
    506
    Mikel Lindsaar
    Mar 31, 2008
  2. Old Echo
    Replies:
    1
    Views:
    187
    Adam Shelly
    Sep 4, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    379
    7stud --
    Aug 21, 2009
  4. David Wake

    Conditional options to a regexp match?

    David Wake, Oct 10, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    105
    John W. Krahn
    Oct 11, 2003
  5. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page