正则表达式匹配文本中的特定数字

时间:2011-07-25 17:14:50

标签: c# regex list foreach match

我有一个看起来像这样的文件:

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    

我需要单独抓取第3列第4列并将它们存储到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 的正则​​表达式存储到一个列表中。我想将两列值分开存储。 所以 X 的一个列表和 Y 的一个列表。


问题:

  • 我可以使用什么正则表达式匹配第3列数字并将其存储在 “X” 以及(或单独使用其他正则表达式)匹配第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");
            }
        });
    }

2 个答案:

答案 0 :(得分:3)

为了让事情变得简单我已经命名了这些组,但以下内容应该有效:

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

请注意,这需要第3和第3版。第4列有十进制数字(我不做任何检查接受整数或十进制数,但如果有必要,可以添加)。

请注意以上has been tested可行。

<强>摘要

基本上我们使用你拥有的捕获,但是使用捕获组(括号)扩展它。我还使用命名组(组的开头为?<x>?<y>),因此您可以分别使用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"

答案 1 :(得分:0)

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

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