我想通过正则表达式执行此操作: 样品: 父亲今天去拜访他的祖父和他的父亲。
如何通过正则表达式选择粗体两个父亲?非常感谢。
答案 0 :(得分: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");
这不是一个非常有趣的正则表达式,因为它只是一个明确的字符串文字。
答案 1 :(得分:1)
试试这个
var matchs=Regex.matches("father goes visit his grandfather and and his father today.",@"\bfather\b");
答案 2 :(得分: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
}