[英]Regular expression replace text contents
I have this paragraph: 我有这段:
"This person name #Question1#, #Question2#, has #Question3# been drinking in the past two days."
I use regex to find an array of matching entries for #Question[0-9]+#
, my question is, how can utilise the regex feature to replace these #Question[0-9]+#
with actual answers from my database. 我使用正则表达式查找
#Question[0-9]+#
的匹配条目数组,我的问题是,如何利用正则表达式功能用数据库中的实际答案替换这些#Question[0-9]+#
。
Here is my code 这是我的代码
const string pattern = @"#Question([0-9]+)#";
string input = template.GetPrintOut;
MatchCollection matches = Regex.Matches(input, pattern);
I can provide a dictionary of replacing string from database. 我可以提供从数据库替换字符串的字典。 Any ideas?
有任何想法吗?
Of course, use Regex.Replace
: 当然,请使用
Regex.Replace
:
var res = reg.Replace(text, match => { ....; return "reply"; });
In the match lambda you can recover the data based on the match
value and return the reply accordingly. 在匹配lambda中,您可以根据
match
值恢复数据并相应地返回答复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.