1

我想通过正则表达式做到这一点:示例: 父亲今天去拜访他的祖父和他的父亲

如何通过正则表达式选择粗体的两个父亲?多谢。

4

3 回答 3

1

我不确定为什么你需要正则表达式,因为你可以简单地“选择”你想要的字符串:

String myStr = "father";

或者,如果您需要替换字符串中的子字符串,您可以执行以下操作:

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

但是,如果你真的想使用 RegEx,你可以MatchCollection这样做:

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

这不是一个非常有趣的正则表达式,因为它只是一个明确的字符串文字。

于 2013-04-21T09:33:29.810 回答
1

尝试这个

var matchs=Regex.matches("father goes visit his grandfather and and his father today.",@"\bfather\b");
于 2013-04-21T09:35:30.320 回答
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
}
于 2013-04-21T10:02:19.220 回答