Newbie: Simple conditional on regexp match

  • Thread starter Kenneth Brun Nielsen
  • Start date
K

Kenneth Brun Nielsen

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";
}
}
 
K

Kenneth Brun Nielsen

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.
 
X

xhoster

Kenneth Brun Nielsen said:
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.
 
S

szr

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.)

So it should be:

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top