in reply to Quarms with RegEx

I think the line:
if ($curline !~ /^#\s/ or $curline !~ /^\s/)
is wrong.

You almost never want two negative conditions combined with 'or'.

Try 'and' between the conditions:

if ($curline !~ /^#\s/ and $curline !~ /^\s/)

Replies are listed 'Best First'.
Re: Re: Quarms with RegEx
by blakem (Monsignor) on Aug 28, 2001 at 12:53 UTC
    nice catch wise monk
    if ($curline !~ /^#\s/ or $curline !~ /^\s/)
    is logically equivalent to:
    if (1)
    because every string will either A.) not start with a '#' or B.) not start with a space.

    Might I recommend a smarter regex rather than anding two simple ones together?

    if ($curline !~ /^#?\s/)

    -Blake

      if ($curline !~ /^#?\s/)
      That will not work, because a domain line looks like:
      #domain1
      And that will not match your regex, so the condition is true, which is wrong.
        Ok, what am I missing here....
        #!/usr/bin/perl -w use strict; for ('#abc',' abc','abc','# abc',' #abc') { my $v1 = ($_ !~ /^#\s/ and $_ !~ /^\s/) || 0; # yours my $v2 = ($_ !~ /^#?\s/) || 0; # mine print "$v1 $v2 '$_'\n"; }
        Output:
        1 1 '#abc' 0 0 ' abc' 1 1 'abc' 0 0 '# abc' 0 0 ' #abc'

        -Blake

Re: Re: Quarms with RegEx
by stefan k (Curate) on Aug 28, 2001 at 12:47 UTC
    just because the phone rang while I was in the process of writing ... :)