能不能实现用正则表达式替换for为foreach
发布网友
发布时间:2024-02-23 04:42
我来回答
共1个回答
热心网友
时间:2024-07-19 17:08
C#编程:(当然首先你得把文本中的字符读入字符串input中)
Regex rex = new Regex(@"(?!\s)_(?<number>\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string input = "yy_1323 ljdljtoe dd_35djlgjoejs_33";
string afterRepl = "";
MatchCollection matches = rex.Matches(input);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
GroupCollection group = match.Groups;
afterRepl = rex.Replace(input, "[" + group["number"].Value + "]");
}
}
afterRepl就是你要的结果字符串