in reply to RegEx Beginning of line or whitespace match

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.

Replies are listed 'Best First'.
Re^2: RegEx Beginning of line or whitespace match
by THRAK (Monk) on Feb 11, 2005 at 21:46 UTC
    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