2014-04-26 70 views
0

我在c#代码中有以下问题。边界字符不工作在c#

class Program 
    { 
     static void Main(string[] args) 
     { 
     string line="have a nice day with nice(songs)"; 
     string [email protected]"\bnice\b"; 
     string replaced_line=Regex.Replace(line, pattern, "Good", RegexOptions.IgnoreCase); 
} 
} 

输出 - :有很好的(歌曲)的好日子

以上输出是罚款。但下面的代码是不是更换

class Program 
    { 
     static void Main(string[] args) 
     { 
     string line="have a nice day with nice(songs)"; 
     string to_replace="nice"; 
     string replaced_line=Regex.Replace(line, @"\b"+to_replace+"\b", "Good", RegexOptions.IgnoreCase); 
} 
} 

输出 - :有一个愉快的一天漂亮(听歌)

它不更换

任何人都可以请帮助我。

+0

排印错误:缺少'@' - '“\ b “'不同于'@”\ b“'。 –

+0

你可以请重复代码与更改 – Axs

+0

@ Manas看到我的回答 –

回答

0

此行是错误的

string replaced_line=Regex.Replace(line, @"\b"+to_replace+"\b", "Good", RegexOptions.IgnoreCase); 

你逃避它只有1次,但你必须这样做每个字符串。所以应该是

string replaced_line=Regex.Replace(line, @"\b"[email protected]"\b", "Good", RegexOptions.IgnoreCase); 

(添加@)

希望工程

+0

棒极了!谢谢你,先生 – Axs