Search code examples
c#regexcompiler-constructionlexical-analysis

Regular Expression excluding of expression like int keyword


I've written a mini scanner for a compiler and it reads from a file and I want to write instead of variables, id and for keywords do nothing (a group of words that want exclude from my variable form expressions), below line get my variable, how can I exclude for example int or bool or ... from this expression:

((?:[A-Za-z][A-Za-z0-9_]*))

Solution

  • What you want is a zero width negative lookahead I think. The syntax in C# regex is to group the expression you don't want to match in a zero width negative lookahead. Also you probably need to delimit the expressing with word boundaries \b:

    string pattern = @"(\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b)";