2014-02-26 92 views
0

此代碼不起作用,但它適用於其他表達式,如(?:[A-Za-z][A-Za-z0-9_]*)用正則表達式替換C#

以下表達式工作在正則表達式測試儀correctley,但它並沒有在此代碼ID替換您好:

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); 

回答

4

你應該逃避backslashes或使用string@開始並使其verbatim string\bC#backspace特殊的含義,請參閱文檔:Escape Sequences

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