http://www.perlmonks.org?node_id=430286

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

I know I should be able to figure this out, but I'm kind of "code blind" at this point. I need to match "for(", but not if it is preceeded by anything other than whitespace or the beginning of the line. I can easily match the whitespace/non-whitespace, but I can't seem to figure out the "or" BOL part. Here's basic snippet of what I'm trying to do:
#!/usr/bin/perl -w use strict; use diagnostics; my $line = 'some_for($var)'; #don't match $line = ' for($var)'; #match $line = 'for($var)'; #match if ($line =~ /\s+(for\()/gi) { print 'match'; }
Changing \s+ to \s* obviously makes it match the BOL, but it also makes it match the non-whitespace also.

Replies are listed 'Best First'.
Re: RegEx Beginning of line or whitespace match
by saintmike (Vicar) on Feb 11, 2005 at 21:37 UTC
    Typically, the word boundary anchor \b works well in these cases:
    if($line =~ /\bfor\(/) { # ... }
    It will also match "(for(" and ";for(" which is a very common requirement for parsing programming languages. Speaking of which, if you are trying to parse perl or parts of it, check out PPI.
      See, I knew this wasn't as difficult to the level I was struggling with it. This works, because I forgot two additional test cases...that being that this could be anywhere in the line, not just at the beginning with or without spaces. e.g.
      $line = 'could be anything here some_for($var)'; #don't match $line = 'could be anything here for($var)'; #match
      Thanks.
        In other words: match if for( begins at a word boundary, don't match in all other cases:
        m/\b(for\()/
        PS: you can drop the parentheses if you don't want to extract anything and only want to test for matches.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: RegEx Beginning of line or whitespace match
by eieio (Pilgrim) on Feb 11, 2005 at 21:33 UTC
    /^\s*(for\()/ should work. Use '^' to require that the match occurs at the beginning of a line rather than anywhere within the line.
Re: RegEx Beginning of line or whitespace match
by gaal (Parson) on Feb 11, 2005 at 21:35 UTC
    /^\s*for\(/

    This matches if the "for(" has no characters at all before it (== at beginning of line) or if there are whitespace characters. You were missing the anchor, I think.

Re: RegEx Beginning of line or whitespace match
by Frantz (Monk) on Feb 11, 2005 at 21:33 UTC
    try with this :

    if ($line =~ /(\s+|^)(for\()/gi) { print 'match'; }
    • I do not think the alternation of space with "start of line" is a good thing .
      It's not good style and it looks like it can cause problems.
      Use ^\s* instead .

    • You don't need to remember the patterns so why use () to match the spaces ?

    • What would the /g modifier do in such a context ? I don't like it with a start of line.

      Regards,
      ZlR.

Re: RegEx Beginning of line or whitespace match
by holli (Abbot) on Feb 11, 2005 at 21:38 UTC
    use strict; use diagnostics; my @line = ( 'some_for($var)', #don't match ' for($var)', #match 'for($var)', #match ); for ( @line ) { print /(^(\s+)?)for\(/gi ? "match\n" : "nomatch\n"; }
    holli, /regexed monk/
Re: RegEx Beginning of line or whitespace match
by Anonymous Monk on Feb 12, 2005 at 15:04 UTC

    Hi, simple try this

    use strict; use diagnostics; my $line = 'some_for($var)'; #don't match $line = ' for($var)'; #match $line = 'for($var)'; #match if($line =~ m#^(\s+)?for\(#si) { print "Match"; } else { print "Non-match"; }

    Gubendran.L

Re: RegEx Beginning of line or whitespace match
by gube (Parson) on Feb 12, 2005 at 15:05 UTC

    Hi try this simple

    use strict; use diagnostics; my $line = 'some_for($var)'; #don't match $line = ' for($var)'; #match $line = 'for($var)'; #match if($line =~ m#^(\s+)?for\(#si) { print "Match"; } else { print "Non-match"; }

    Gubendran.L