【问题标题】:Regular Expression matching a certain number in the text正则表达式匹配文本中的某个数字
【发布时间】:2011-10-12 18:54:23
【问题描述】:

我有一个如下所示的文件:

STUFF   STUFF       **X**     **Y**  STUFF STUFF
J6      INT-00113G  227.905    5.994  180  SOIC8    
J3      INT-00113G  227.905 -203.244  180  SOIC8     
U13     EXCLUDES    -42.210  181.294  180  QFP128    
U3      IC-00276G     5.135  198.644  90   BGA48     
U12     IC-00270G  -123.610 -201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    

我需要分别抓取第三列第四列并将它们存储到C#中的列表中。


我目前正在匹配 第 3 列和第 4 列,使用:

        var xyResult = new List<string>();
        var mainResult = new List<string>();

        foreach (var mainLine in fileList)
            mainResult.Add(string.Join(" ", mainLine));

        foreach (var xyLine in mainResult)
        {
            Match xyRegex = Regex.Match(xyLine, @"[\d]+\.[\d]+\s+[\d]+\.[\d]+");

            if (xyRegex.Success)
            {
                xyResult.Add(string.Join(" ", xyRegex));
            }
        }

        List<string> finalXYResult = xyResult.ToList();

        foreach (var line in finalXYResult)
            displayXYRichTextBox.AppendText(line + "\n");

现在我将匹配 X 和 Y 的正则表达式存储到 one 列表中。我想将两列值分开存储。 所以X 一个列表,Y 一个列表。


问题:

  • 我可以使用什么正则表达式来匹配第三列数字并将其存储在 "X"(或单独使用另一个正则表达式)中 em> 匹配第 4 列数字并将其存储在 "Y"?

编辑:

    private void calculateXAndYPlacementTwo()
    {
        // Reads the lines in the file to format.
        var fileReader = File.OpenText(filePath + "\\Calculating X,Y File.txt");

        // Creates a list for the lines to be stored in.
        var fileList = new List<string>();

        // Adds each line in the file to the list.
        while (true)
        {
            var line = fileReader.ReadLine();
            if (line == null)
                break;

            fileList.Add(line);
        }

        // Creates new lists to hold certain matches for each list.
        var xyResult = new List<string>();
        var mainResult = new List<string>();
        var xResult = new List<string>();
        var yResult = new List<string>();

        foreach (var mainLine in fileList)
            mainResult.Add(string.Join(" ", mainLine));

        mainResult.ForEach(xyLine =>
        {
            Match xyRegex = Regex.Match(xyLine, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
            if (xyRegex.Success)
            {
                String xValue = xyRegex.Groups["x"].Value;
                String yValue = xyRegex.Groups["y"].Value;

                xyResult.Add(String.Join(" ", new[]{ xValue, yValue }));

                foreach (var line in xValue)
                    richTextBox1.AppendText(line + "\n");

                foreach (var line in yValue)
                    richTextBox2.AppendText(line + "\n");
            }
        });
    }

【问题讨论】:

    标签: c# regex list foreach match


    【解决方案1】:

    为了方便起见,我为这些组命名,但以下内容应该可以:

    (?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)
    

    请注意,这要求第 3 列和第 4 列都包含小数(我不做任何检查以接受整数或小数,但如有必要,可以添加)。

    注意上面的has been tested 工作。

    总结

    基本上我们使用您拥有的捕获,但使用捕获组(括号)扩展它。我还使用命名组(组开头的?&lt;x&gt;?&lt;y&gt;),因此您可以分别引用使用xyRegex.Groups["x"]xyRegex.Groups["y"] 找到的值。

    我还发现当数字出现负值时您的捕获失败,因此我在模式中添加了一个可选的负符号 (-?) 来解决这个问题。

    所以,分解,这里是声明:

    (?<x>              # Begin capture group "x"
      -?                 # allow a negative symbol 0 or 1 time
      \d+                # allow 1+ numbers
      \.                 # allow a single decimal
      \d+                # allow decimal numbers 1+ times
    )                  # end capture group "x"
    \s+                # allow white space between the number sets
    (?<y>              # Begin capture group "y"
      -?                 # \
      \d+                #  | - same as above
      \.                 #  |
      \d+                # /
    )                  # End capture group "y"
    

    【讨论】:

    • 您能解释一下这是如何工作的吗?我怎么能在我当前的代码中实现它?..此外,没有一个实际的行来标记它STUFF, STUFF, ***X***, ***Y***, STUFF, STUFF。只有数据而不是“标签”
    • 谢谢。我尝试了您的演示,但我使用的是文件而不是示例字符串。而且我没有写入控制台,而是输出到 2 个富文本框。我的问题现在似乎可以正确输出到richtextboxes。用richTextBox1.AppendText(xValue + "\n"); richTextBox2.AppendText(yValue + "\n"); 替换您的Console.WriteLine() 似乎不会“显示” RTB 中的任何值。你知道如何解决这个问题吗?
    • @Colton:this version 怎么样? -- 或 BETTER YET 我将其分解为您已经在使用的格式,包括遍历字符串列表。您可能也不想转换为双精度值,并且可能只想在将值推送到 RTB 之前将它们保持为字符串格式。
    • 这个好像有点问题哈哈
    • @Colton:麻烦在哪方面?要么在这里解释,要么发布更新您的问题。
    【解决方案2】:

    您只需要使用正则表达式组,然后从匹配结果中获取各个元素。

    foreach (var xyLine in mainResult) {
        //PLACEMENT TWO Regex
        Match xyRegex = Regex.Match(xyLine, @"([\d]+\.[\d]+)\s+([\d]+\.[\d]+)");
    
        if (xyRegex.Success) {
            xResult.Add(xyRegex.Groups[1].Value);
            yResult.Add(xyRegex.Groups[2].Value);
        }
    }
    

    【讨论】:

    • 它报错,指出.Groups不能像方法一样使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多