I have a file that looks like this:
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
And I need to grab the 3rd column and the 4th column seperately and store them into a list in C#.
I am currently matching the 3rd and 4th column together using:
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");
Right now I am storing the regex matching both X and Y into one list. I would like to store the two column values seperate. So, one list for X and one list for Y.
QUESTION:
- What regex can I use to match the 3rd column of numbers and store it in “X” and also (or seperately using another regex) match the 4th column of numbers and store it in “Y”?
EDIT:
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");
}
});
}
To make things easier I’ve named the groups, but the following should work:
Please note that this requires both the 3rd & 4th column to have decimal numbers (I don’t do any checking to accept either whole or decimal numbers, but if necessary, this can be added).
Note the above has been tested to work.
Summary
Basically we use the capture you had, but extend it using capture groups (the parenthesis). I also use named groups (the
?<x>and?<y>at the start of the group) so you can reference the values found usingxyRegex.Groups["x"]andxyRegex.Groups["y"], respectively.I also found your capture failed when numbers appeared with negative values, so I added an optional negative symbol (
-?) to the pattern to account for that.So, broken down, here is the statement: