普通防爆pression大写更换在C# [英] Regular Expression Uppercase Replacement in C#
问题描述
我有以下的C#这只是替换输入字符串的部分看起来像装备:19d005到URL中,像这样的:
I have the following C# which simply replaces parts of the input string that look like EQUIP:19d005 into URLs, like this:
input = Regex.Replace(input, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);
的HTML最终看起来是这样的。
The HTML ends up looking like this.
<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19d005">EQUIP:19d005</a>
唯一的问题是,在目标页面预计eqnum查询字符串,以全部大写,因此返回正确的设备时,eqnum = 19D005,但未能在收到eqnum = 19d005。
The only trouble is that the destination page expects the eqnum querystring to be all UPPERCASE so it returns the correct equipment when eqnum=19D005 but fails if it receives eqnum=19d005.
我想我可以修改和完全正确EquipmentDisplay.asp的错误的大写值的要求,如果可能的话,我想将C#code遵守现有的传统的ASP页面由Regex.Replace uppercasing的$ 2上述表示的。
I guess I can modify and correct EquipmentDisplay.asp's errant requirement of uppercase values however, if possible I'd like to make the C# code comply with the existing classic ASP page by uppercasing the $2 in the Regex.Replace statement above.
在理想情况下,我想返回的HTML看起来是这样的:
Ideally, I'd like the HTML returned to look like this:
<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19D005">EQUIP:19d005</a>
的通知虽然原始字符串是装备:19d005(小写),只有eqnum =值是大写的
能不能做到,如果是这样,什么是做到这一点的方式最整洁的?
Can it be done and if so, what's the tidiest way to do it?
推荐答案
确定; 2解决方法,一是行内:
OK, 2 solutions, one inline:
input = Regex.Replace(input, @"(EQUIP:)(\S+)", m => string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", m.Groups[1].Value, m.Groups[2].Value, m.Groups[2].Value.ToUpper()), RegexOptions.IgnoreCase);
另外用一个单独的函数:
The other using a separate function:
var input = Regex.Replace(input, @"(EQUIP:)(\S+)", Evaluator, RegexOptions.IgnoreCase);
private static string Evaluator(Match match)
{
return string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", match.Groups[1].Value, match.Groups[2].Value, match.Groups[2].Value.ToUpper());
}
这篇关于普通防爆pression大写更换在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!