簡體   English   中英

正則表達式替換文本內容

[英]Regular expression replace text contents

我有這段:

"This person name #Question1#, #Question2#, has #Question3# been drinking in the past two days."

我使用正則表達式查找#Question[0-9]+#的匹配條目數組,我的問題是,如何利用正則表達式功能用數據庫中的實際答案替換這些#Question[0-9]+#

這是我的代碼

        const string pattern = @"#Question([0-9]+)#";
        string input = template.GetPrintOut;
        MatchCollection matches = Regex.Matches(input, pattern);

我可以提供從數據庫替換字符串的字典。 有任何想法嗎?

當然,請使用Regex.Replace

var res = reg.Replace(text, match => { ....; return "reply"; });

在匹配lambda中,您可以根據match值恢復數據並相應地返回答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM