用regEx匹配替换字符串 [英] Replace string, with regEx match
本文介绍了用regEx匹配替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用RegEx进行捕获
以下模式:
i wish to make a capture using RegEx
following pattern:
string pattern = @"/[a-z]{2}/products/\d{4}/\(?<fileName>[abc])\.aspx";
string inputString = longString(around 3000 characters);
但是我需要利用这个捕获传递给构造函数
but i need to utilise this capture to pass into a constructor
Object obj = new Obj(capture)
为了加载对象并检索该对象的属性(guid),显然这个guid对于每场比赛都会有所不同。
i需要对regEx模式的每个匹配执行此操作,然后用
in order to load the object and retrieve a property (guid) of that object, obviously this guid will be different for every match.
i need to do this for every match on the regEx pattern, and then replace input string with
"info.aspx?number" + guid;
然后返回完成所有替换的输入字符串。
and then return the input string with all replacements done.
推荐答案
Regex.Replace有一个重载,它采用匹配求值程序方法:
Regex.Replace has an overload that takes a match evaluator method:
string output = Regex.Replace(input, pattern, ReplaceMethod);
}
private string ReplaceMethod(Match m)
{
MyObject obj = new MyObject(m.Value);
return "info.aspx?number" + obj.Guid.ToString();
}
这篇关于用regEx匹配替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文