Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie: Simple conditional on regexp match

0 views
Skip to first unread message

Kenneth Brun Nielsen

unread,
Aug 6, 2008, 5:54:06 AM8/6/08
to
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

unread,
Aug 6, 2008, 9:08:33 AM8/6/08
to
On Aug 6, 11:54 am, Kenneth Brun Nielsen

DOH! I found out that the above works as intended. I just "forgot" to
escape the | in the regexp.

xho...@gmail.com

unread,
Aug 6, 2008, 12:27:06 PM8/6/08
to
Kenneth Brun Nielsen <kenneth.br...@googlemail.com> 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.

szr

unread,
Aug 16, 2008, 2:53:16 PM8/16/08
to
xho...@gmail.com wrote:
> Kenneth Brun Nielsen <kenneth.br...@googlemail.com> 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


0 new messages