2013-04-21 49 views

回答

1

我不知道爲什麼你需要爲這個正則表達式,因爲你可以「選擇」你想要的字符串簡單地做:

String myStr = "father"; 

另外,如果您需要更換的子字符串中,你可以做到以下幾點:

String myStr = "father goes visit his grandfather and and his father today." 
myStr = myStr.Replace(" father ", " someOtherString "); 

但是,如果你真的使用正則表達式,你可以通過執行獲得MatchCollection

String myStr = "father goes visit his grandfather and and his father today." 
Regex.Mathces(myStr, "\bfather\b"); 

這不是一個非常有趣的正則表達式,因爲它只是一個明確的字符串文字。

1

試試這個

var matchs=Regex.matches("father goes visit his grandfather and and his father today.",@"\bfather\b"); 
1
var matchCollection = Regex.Matches("father goes visit his grandfather and and his father today.", "\bfather\b"); 

foreach (Match m in matchCollection) 
{ 
    // Process your code for each match 
}