【问题标题】:Extracting a number from a specific substring pattern从特定子字符串模式中提取数字
【发布时间】:2019-10-30 03:26:22
【问题描述】:

已解决!最后用它来做我想做的事:

if (output.Contains("<%TABLE#"))
    {
         string pattern = @"<%TABLE#([0-9]+)%%>";
         RegexOptions options = RegexOptions.Multiline;

         foreach (Match m in Regex.Matches(output, pattern, options))
         {
               int objectID = Int32.Parse(Regex.Match(m.Value, @"\d+").Value);
               output = output.Replace(m.Value, ConvertFunction(objectID));
         }
    }

在我的 SQL 数据的某些部分(由 C#/ASP.NET 网站前端呈现)是字符串,其中许多可以包含类似 - [NUMBER] 的模式总是一个特定的 ID,1+。一个字符串中的示例类似于 。有时,同一字符串中可能有多个这些模式。我最终试图做到以下几点:

  1. 查找字符串中出现模式的所有实例
  2. 对于每个实例,使用存在的 # 调用另一个构建函数 - 它使用该 # 并生成一个 NewString
  3. 用 NewString 替换该代码实例

我这样做是因为每个 SQL 表都有一个网站页面来向最终用户显示其中的数据。我格式化数据的方式因表而异,因此我为每个表都有一个类,用于构建输出字符串并在需要的地方返回它。有时,我需要显示当前表中不同表中的对象。为了尝试解决这个问题,我在字符串中添加了上面的公式,在我想要渲染对象的特定位置,希望识别它并使用其中的 ID 来获取适当的对象,构建它,然后替换它模式。

我猜我将不得不使用正则表达式或其他东西来识别字符串,但我正在努力寻找最佳方式来获取模式、识别其中的数字、调用函数以使用所述来呈现输出文本编号,然后用结果替换该特定模式。

以下是一些示例输入和输出应该是什么。函数 ConvertFormula 接受一个 INT 并输出一个 STRING。

示例输入/预期输出

示例 1: "Here's some data and more stuff.<%TABLE#3541%%>Here is more text. <%TABLE#31214%%>And some more."

输出 1: "Here's some data and more stuff." + ConvertFormula(3541) + "Here is more text." + ConvertFormula(31214) + "And some more."

示例 2: "Here's some data and more stuff! Maybe more here!&lt;%TABLE#2%%&gt;Here is more text."

输出 2: "Here's some data and more stuff! Maybe more here!" + ConvertFormula(2) + "Here is more text."

示例 3: "&lt;%TABLE#2%%&gt;This is something completely different with the object call at the start.&lt;TABLE#52%%&gt; and another here."

输出 3: ConvertFormula(2) + "This is something completely different with the object call at the start." + ConvertFormula(52) + " and another here."

示例 4: "There's nothing in this one, no code to find. Just has some text."

输出 4: "There's nothing in this one, no code to find. Just has some text."

示例 5: "This one goes on for a while, like 5132854123 characters, then has a single call right here.&lt;%TABLE#112%%&gt;"

输出 5: "This one goes on for a while, like 5132854123 characters, then has a single call right here." + ConvertFormula(112)

示例 6: "Short &lt;%TABLE#412%%&gt; one."

输出 6: "Short " + ConvertFormula(412) + " one."

示例 7: "Nothing here again."

输出 7: "Nothing here again."

【问题讨论】:

  • 再次编辑它 - 尽可能清楚地说明示例。准确显示每个输入/输出应该是什么。 ConvertFormula 逻辑并不重要,它只需要一个 INT 并返回一个 STRING。

标签: c# regex linq


【解决方案1】:

我猜这个表达式可能很简单,

<%TABLE#([0-9]+)%%>

我们将使用捕获组并收集我们想要的 ID。

Demo

测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"<%TABLE#([0-9]+)%%>";
        string input = @"<%TABLE#3%%>
<%TABLE#1213%%>";
        RegexOptions options = RegexOptions.Multiline;
        
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

【讨论】:

  • 这最终非常有帮助!我用它来找出解决方案,现在编辑我的初始帖子以显示它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多