-3

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_]*))
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

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)";
John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • your expression for example just omit 'b' from bool but i want to omit all "bool" as a word. – user3356317 Feb 26 '14 at 15:03
  • please help me because im new at regular expression and have a project on it,thanks. – user3356317 Feb 26 '14 at 15:09
  • @user3356317 I updated the example with an expression I tested. – John Weldon Feb 26 '14 at 15:27
  • bu when i wnat add it to my c# code it doesen't replace correctly but in a online regular expression tester it works,my code is: – user3356317 Feb 26 '14 at 15:48
  • string pattern = "(\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b)"; string replacement = "id"; Regex rgx = new Regex(pattern); this.lines[index] = rgx.Replace(this.lines[index], replacement); – user3356317 Feb 26 '14 at 15:49
  • You'll have to be more specific... it works for me. Every word in the line that is not a keyword gets replaced with the string "id" – John Weldon Feb 26 '14 at 16:14
  • please test below code,it doesn't work with this regular expression but work with pas expressions: – user3356317 Feb 26 '14 at 17:10
  • string test = "int Hello := 2 ;"; string pattern = "\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b"; string replacement = "Id"; Regex rgx = new Regex(pattern); string newline = rgx.Replace(test, replacement); – user3356317 Feb 26 '14 at 17:15
  • The issue is that your `\b` is being escaped before being handled by Regex. You need to either make the string a string literal by prefixing with @ like this: (`string pattern = @"\b(?!int|bool)(?:[A-Za-z][A-Za-z0-9_]*)\b"`), or double escape the `\b` like this: `\\b` – John Weldon Feb 26 '14 at 17:48