Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need a regex to determing if an array item begins with a #. For some reason my regex doesn't work, ofcourse I've never seen !=~ before but I assume it's functional. It doesn't error out, so that's all I know.
foreach(@lines) { if ($_ !=~ m/^#/g) { print "yahoo!"; } }

Replies are listed 'Best First'.
Re: Regex to check for beginning character
by Limbic~Region (Chancellor) on Mar 05, 2004 at 15:12 UTC
    Anonymous Monk,

    =~ says: matches regex
    !~ says: does not match regex

    See perldoc perlop for more info. Either of the following should work:
    print "yahoo!\n" if grep /^#/ , @lines; # or for ( @lines ) { if ( /^#/ ) { print "yahoo!\n"; last; } }
    The latter stopping on first match.

    Cheers - LR

Re: Regex to check for beginning character
by perrin (Chancellor) on Mar 05, 2004 at 15:33 UTC
    You don't need a regex for an exact match like this.

    foreach(@lines) { if ( index( $_, '#' ) == 0 ) { print "yahoo!"; } }
    That's much faster than using a regex for this.
      And of course you can simplify this to
      foreach(@lines) { print "yahoo!" if ( index( $_, '#' ) == 0 ); }
        That's not simplification. You're trying to win style points, but that's not how it's done (use parens only when neccessary) :)
        for(@lines){ print "yahoo!" if 0 == index $_, '#'; }
Re: Regex to check for beginning character
by davido (Cardinal) on Mar 05, 2004 at 16:44 UTC
    There are a couple of problems. First (in order of appearance), you don't need the /g modifier. You're only looking for the first case in each array element, so /g is useless. Second, !=~ isn't really what you're looking for. If you want it to return true when a match occurs, use =~ If you want it to return false when a match occurs, use !~

    Also, if you leave off the =~ operator, $_ is implicit, so you could actually just say if ( m/^#/ ) {.....


    Dave

Re: Regex to check for beginning character
by bart (Canon) on Mar 06, 2004 at 00:23 UTC
    I've never seen !=~ before but I assume it's functional.
    ... except it doesn't do what you want. What you've got here is a combination of two operatores: != and ~. What you're asking for is to test that $_ is not numerically the same value as the result of m/^#/g, bitwise inverted. You can test my claim with the next snippet:
    use constant NOT1 => ~ 1; print "~1 is " . NOT1 . "\n"; if(NOT1 !=~ 1) { print "Different (huh?!?)\n"; } else { print "The same (of course!)\n"; }
    which results in:
    ~1 is 4294967294
    The same (of course!)
    
    For any other number than 1 in the conditional, the test says "different".

    p.s. Like everybody else seems to have told you already, you're looking for !~. Or, you can use the inverted test, using unless, instead of if

Re: Regex to check for beginning character
by UnderMine (Friar) on Mar 05, 2004 at 17:38 UTC
    What about leading whitespace? Assuming you want to return the items starting with a # then :-
    @lines = (' #', 'nope', '# yep', 'oops #'); foreach(@lines) { print "yahoo! $_\n" if (/^\s*#/); }
    Just a thought
    UnderMine
Re: Regex to check for beginning character
by ysth (Canon) on Mar 21, 2004 at 05:13 UTC
    In the current development version of perl, this will now give a warning:
    !=~ should be !~
Re: Regex to check for beginning character
by slloyd (Hermit) on Mar 05, 2004 at 20:05 UTC
    foreach(@lines){ if($_!~/^\#/s){print "does not start with a pound sign";} else{print "begins with a pound sign\n";} }
Re: Regex to check for beginning character
by ysth (Canon) on Mar 09, 2004 at 04:09 UTC
    Has anyone else ever said !=~ instead of !~ ? Should this give a syntax warning? Newbies especially don't be afraid of speaking up.