【问题标题】:Regular Expression Uppercase Replacement in C#C# 中的正则表达式大写替换
【发布时间】:2010-09-17 08:24:16
【问题描述】:

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

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

HTML 最终看起来像这样。

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

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

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

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

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

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

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

【问题讨论】:

    标签: c# .net regex


    【解决方案1】:

    好的,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);
    

    另一个使用单独的函数:

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

    【讨论】:

    • one-liner 是一种非常优雅的解决方案,顺便说一句,效果很好。 @Vinko 提到使用匿名函数但没有给出示例,因此我选择了您的解决方案。当我看到 => 用于 LINQ to SQL、表达式树和现在的匿名函数时,我有点困惑?为什么有这么多用途?
    • => 样式语法是 delegate() { } 语法的较短版本。其他优点是,如果您有一个像上面这样的衬里,则不需要 return 语句并且类型是推断出来的 - 所以我只指定 m 而不是 Match m。 lambda 语法有助于使代码更简洁。
    【解决方案2】:

    直接使用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());
    

    【讨论】:

      【解决方案3】:

      您可以在替换中使用 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());
       }
      

      【讨论】:

        【解决方案4】:

        假设输入是一个字符串:

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

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

        【讨论】:

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

          【讨论】:

            【解决方案6】:
            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>");
            }
            

            【讨论】:

              猜你喜欢
              • 2018-04-07
              • 2018-12-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-03-21
              • 2022-06-12
              相关资源
              最近更新 更多