RegEx替换在短语周围添加括号
本文关键字:周围添 加括号 短语 替换 RegEx | 更新日期: 2023-09-27 18:07:07
我需要一个RegEx,当遇到逗号时将括号添加到单词字符串中。下面的例子可能更简单:
I was here,there -> (I was here),there
Bob,here you there,somewhere——>Bob, (here you there),somewhere
Sue,Bob,Joe,here thee here——>Sue,Bob,Joe, (here thee here)
Milly Barry Molly,Joe Sandy Mary -> (Milly Barry Molly),(Joe Sandy Mary)
任何帮助或指点将不胜感激
Try
string input = "Milly Barry Molly,Joe Sandy Mary";
Regex regex = new Regex(
@"(?<=^|,)'s*(?>[^'s,]+'s*){2,}(?=,)|(?<=,)'s*(?>[^'s,]+'s*){2,}$" );
string result = regex.Replace(input, "($&)");
Console.WriteLine(result);
// (Milly Barry Molly),(Joe Sandy Mary)
根据要求进一步解释