[英]How can i create this regex about this condition
我想通過正則表達式來做到這一點:示例: 父親去看望他的祖父和今天他的父親 。
如何通過正則表達式選擇粗體兩個父親? 非常感謝。
我不確定為什么你需要正則表達式,因為你可以通過這樣做“選擇”你想要的字符串:
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");
它不是一個非常有趣的正則表達式,因為它只是一個明確的字符串文字。
嘗試這個
var matchs=Regex.matches("father goes visit his grandfather and and his father today.",@"\bfather\b");
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.