C#中的正则表达式大写替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/205382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:58:27  来源:igfitidea点击:

Regular Expression Uppercase Replacement in C#

c#.netregex

提问by Sprogz

I have the following C# which simply replaces parts of the input string that look like EQUIP:19d005 into URLs, like this:

我有以下 C#,它只是将输入字符串中看起来像 EQUIP:19d005 的部分替换为 URL,如下所示:

input = Regex.Replace(input, @"(EQUIP:)(\S+)", @"<a title=""View equipment item "" href=""/EquipmentDisplay.asp?eqnum=""></a>", RegexOptions.IgnoreCase);

The HTML ends up looking like this.

HTML 最终看起来像这样。

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19d005">EQUIP:19d005</a>

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.

唯一的问题是目标页面期望 eqnum 查询字符串全部为大写,因此当 eqnum=19D005 时它返回正确的设备,但如果它收到 eqnum=19d005 则失败。

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.

我想我可以修改和更正 EquipmentDisplay.asp 对大写值的错误要求,但是,如果可能的话,我希望通过在上面的 Regex.Replace 语句中大写 $2 来使 C# 代码符合现有的经典 ASP 页面。

Ideally, I'd like the HTML returned to look like this:

理想情况下,我希望 HTML 返回如下所示:

<a title="View equipment item 19d005" href="/EquipmentDisplay.asp?eqnum=19D005">EQUIP:19d005</a>

Notice although the original string was EQUIP:19d005 (lowercase), only the eqnum= value is uppercased.

请注意,尽管原始字符串是 EQUIP:19d005(小写),但只有 eqnum= 值是大写的。

Can it be done and if so, what's the tidiest way to do it?

可以做到吗?如果可以,最整洁的方法是什么?

采纳答案by Duncan

OK, 2 solutions, one inline:

好的,2 个解决方案,一个内联:

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());
}

回答by John Fiala

Assuming that input is a string:

假设输入是一个字符串:

input = Regex.Replace(input.ToUpper, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);

input = Regex.Replace(input.ToUpper, @"(EQUIP:)(\S+)", @"<a title=""View equipment item $2"" href=""/EquipmentDisplay.asp?eqnum=$2"">$1$2</a>", RegexOptions.IgnoreCase);

Changing the case of a string isn't something regex does.

改变字符串的大小写不是正则表达式所做的。

回答by JaredPar

Using Regex.Replace directly I do not think there is a way. But you could make this a two step process and get the result you are looking for.

直接使用 Regex.Replace 我认为没有办法。但是您可以将此过程分为两步并获得您正在寻找的结果。

var match = Regex.Match(input, @"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
var input = String.Format( @"&lt;a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}""&gt;{0}{1}&lt;/a&gt;", 
match.Groups[1].Value,
match.Groups[2].Value,
match.Groups[2].Value.ToUpper());

回答by Vinko Vrsalovic

You can use a MatchEvaluator delegate instead of a string in the replacement. You can then embed the delegate as an anonymous function if on recent .NET. The 'old' solution might look like something like this:

您可以在替换中使用 MatchEvaluator 委托而不是字符串。如果在最近的 .NET 上,您可以将委托作为匿名函数嵌入。“旧”解决方案可能如下所示:

 static void Main(string[] args)
 {
     string input = "EQUIP:12312dd23";
     string output = Regex.Replace(input, @"(EQUIP:)(\S+)", 
         new MatchEvaluator(genURL), RegexOptions.IgnoreCase);
     Console.WriteLine(output);
     Console.ReadKey();
 }
 static string genURL(Match m)
 {
     return string.Format(@"<a title=""View item {0}"" 
            href=""/EqDisp.asp?eq={2}"">{1}{0}</a>",
            m.Groups[2].Value,m.Groups[1].Value,m.Groups[2].Value.ToUpper());
 }

回答by Jesse C. Slicer

string input = "EQUIP:19d005";
Regex regex = new Regex (@"(EQUIP:)(\S+)", RegexOptions.IgnoreCase);
string eqlabel = regex.Replace(input, "");
string eqnum = regex.Replace(input, "");
string eqnumu = eqnum.ToUpperInvariant();
input = string.Format(@"<a title=""View equipment item {1}"" href=""/EquipmentDisplay.asp?eqnum={2}"">{0}{1}</a>", eqlabel, eqnum, eqnumu);

回答by Vivek

public static string FormatToCustomAnchorTag(this string value)
{

    return Regex.Replace(value.ToLower() + value.ToUpper(),
                @"(?<equiplo>equip:)(?<equipnolo>\S+)(?<equipup>EQUIP:)(?<equipnoup>\S+)",
                @"<a title=""View equipment item ${equipnolo}"" href=""/EquipmentDisplay.asp?eqnum=${equipnoup}"">${equipup}${equipnolo}</a>");
}