Skip to main content
 首页 » 编程设计

c#之用正则表达式和我自己的参数替换字符串

2024年02月13日22wayfarer

在我的 html 中,我有这样的 serval token :

{PROP_1_1}, {PROP_1_2}, {PROP_37871_1} ... 

实际上,我用以下代码替换了该标记:

htmlBuffer = htmlBuffer.Replace("{PROP_" + prop.PropertyID + "_1}", prop.PropertyDefaultHtml); 

其中 prop 是自定义对象。但在这种情况下,它只影响以“_1”结尾的标记。我想将此逻辑传播到所有以“_X”结尾的逻辑,其中 X 是数字。 我如何实现正则表达式模式来实现这一点?

请您参考如下方法:

您可以使用 Regex.Replace():

Regex rgx = new Regex("{PROP_" + prop.PropertyID + "_\d+}"); 
htmlBuffer = rgx.Replace(htmlBuffer, prop.PropertyDefaultHtml);