C# 协助建立正则表达式

C# 协助建立正则表达式,c#,regex,C#,Regex,我需要帮助建立一个正则表达式 在MVC5视图中,我有一个文本区域,其中包含一组或多组整数,每组可以包含6、7或8个字符 在我的控制器中,我需要从输入字符串中提取所有这些数字,并将它们放入一个数组中 例如: 123456 123457 123458 或 123456 123457 123458 或 123456123457、123458 这些组可能有也可能没有1或2个前导零: 0012345600123457123458 这就是我的结局: public string[] ExtractWo

我需要帮助建立一个正则表达式

在MVC5视图中,我有一个文本区域,其中包含一组或多组整数,每组可以包含6、7或8个字符

在我的控制器中,我需要从输入字符串中提取所有这些数字,并将它们放入一个数组中

例如: 123456 123457 123458 或

123456 123457 123458

或 123456123457、123458

这些组可能有也可能没有1或2个前导零:

0012345600123457123458

这就是我的结局:

    public string[] ExtractWorkOrderNumbers(string myText)
    {
        var result = new List<string>();
        var regex = new Regex(@"( |,)*(\d+)");
        var m = regex.Match(myText);

        while (m.Success)
        {
            for (int i = 1; i <= 2; i++)
            {
                var wo = m.Groups[2].ToString();
                if (result.Count == 0)
                {
                    result.Add(wo);
                }
                else
                {
                    var x = (from b in result where b == wo select b).ToList().Count;
                    if (x == 0) result.Add(wo);
                }
            }
            m = m.NextMatch();
        }
        return result.ToArray();
    }
利用正则表达式Regex的特性,我们可以从匹配模式中提取数据。在您的情况下,我们可以提取文本字符串的非零整数部分:

using System.Text.RegularExpressions;

// A pattern consisting of at most two leading zeros followed by 6 to 8 non-zero digits.
var regex = new Regex(@"^[0]{0,2}(?<Value>[1-9]{6,8})$");

var firstString = "123456";
var secondString = "01234567";
var thirdString = "0012345678";

var firstMatch = regex.Match(firstString);
var secondMatch = regex.Match(secondString);
var thirdMatch = regex.Match(thirdString);

int firstValue = 0;
int secondValue = 0;
int thirdValue = 0;

if (firstMatch.Success)
  int.TryParse(firstMatch.Groups["Value"].Value, out firstValue);

if (secondMatch.Success)
  int.TryParse(secondMatch.Groups["Value"].Value, out secondValue);

if (thirdMatch.Success)
  int.TryParse(thirdMatch.Groups["Value"].Value, out thirdValue);

Console.WriteLine("First Value  = {0}", firstValue);
Console.WriteLine("Second Value = {0}", secondValue);
Console.WriteLine("Third Value  = {0}", thirdValue);

假设:零个或多个空格和/或逗号用作分隔符

    [TestMethod()]
    public void TestMethod3()
    {
        var myText = "123456 1234567, 123458, 00123456, 01234567";
        var regex = new Regex(@"( |,)*(\d+)");
        var m = regex.Match(myText);
        var matchCount = 0;
        while (m.Success)
        {
            Console.WriteLine("Match" + (++matchCount));
            for (int i = 1; i <= 2; i++)
            {
                Group g = m.Groups[i];
                Console.WriteLine("Group" + i + "='" + g + "'");
                CaptureCollection cc = g.Captures;
                for (int j = 0; j < cc.Count; j++)
                {
                    Capture c = cc[j];
                    Console.WriteLine("Capture" + j + "='" + c + "', Position=" + c.Index);
                }
            }
            m = m.NextMatch();
        }
    }

正则表达式有什么问题?你试过什么了吗?你试过什么了吗?你的代码有什么问题吗?有些代码片段可能会有帮助,我不确定正则表达式是否是满足您需求的最佳方式。在这种情况下,string.Split后跟int.TryParse似乎就足够了。为什么您认为您需要正则表达式来执行此操作?如果要检查的字符串是123456,则可以使用正则表达式,但如果字符串是123456 123456,则不能使用正则表达式。此外,结果仍然是字符串,而不是整数。碰巧字符串完全由整数组成。它们作为字符串存储在遗留数据库中。在代码中的另一点上,如果用户没有将前导零放入我知道如何操作的位置,我必须添加前导零。啊,您需要将组拆分为单个整数字符串吗?例如,123456 01234567->123456、01234567或者它只是为了验证每一行?我将在数据库查询“where”子句中使用每个整数字符串,类似于:从myTable中选择x、y、z,其中someField='00123456'或someField='00123457'或someField='00123458',然后对捕获的组执行任何您想要的操作。TryParse它们到整数,用前导零填充它们,随便什么。
    [TestMethod()]
    public void TestMethod3()
    {
        var myText = "123456 1234567, 123458, 00123456, 01234567";
        var regex = new Regex(@"( |,)*(\d+)");
        var m = regex.Match(myText);
        var matchCount = 0;
        while (m.Success)
        {
            Console.WriteLine("Match" + (++matchCount));
            for (int i = 1; i <= 2; i++)
            {
                Group g = m.Groups[i];
                Console.WriteLine("Group" + i + "='" + g + "'");
                CaptureCollection cc = g.Captures;
                for (int j = 0; j < cc.Count; j++)
                {
                    Capture c = cc[j];
                    Console.WriteLine("Capture" + j + "='" + c + "', Position=" + c.Index);
                }
            }
            m = m.NextMatch();
        }
    }
Match1
Group1=''
Group2='123456'
Capture0='123456', Position=0
Match2
Group1=' '
Capture0=' ', Position=6
Group2='1234567'
Capture0='1234567', Position=7
Match3
Group1=' '
Capture0=',', Position=14
Capture1=' ', Position=15
Group2='123458'
Capture0='123458', Position=16
Match4
Group1=' '
Capture0=',', Position=22
Capture1=' ', Position=23
Group2='00123456'
Capture0='00123456', Position=24
Match5
Group1=' '
Capture0=',', Position=32
Capture1=' ', Position=33
Group2='01234567'
Capture0='01234567', Position=34