S.Fitz S.Fitz - 1 year ago 64
C# Question

Get specific data from a line of text

I need to extract the following data (in bold) from the text line below and put it into a data grid;

PERS tooldata t_rrt_ja03579:=[TRUE,[[-39.643,-0.001,1025.49],[0.382684,-0.000130001,-0.923889,0.000120001]],[200.9,[-88.1,-12.6,359.7],[1,0,0,0],29.347,50.927,18.261]];

This line is read from a file. I have managed to trim the line so it gets rid of the "PERS tooldata" and whitespaces and it leaves me with the tool name. I have it bound to data in a datagrid elsewhere in the code which is step 1 complete.

My question is how can I extract the values in bold individually and place them in to double data declarations? The first block of values (-39.643,-0.001,1025.49) is a X,Y,Z co-ordinate value and the second (0.382684,-0.000130001,-0.923889,0.000120001) are Q1,Q2,Q3,Q4.

Below is how i done the name

private void AutoFillToolData(object sender, RoutedEventArgs e)
// Gives user option to auto populate datagrid
var AutoFillToolResult = MessageBox.Show("Do you want to auto populate fields?", "Tool Data", MessageBoxButton.YesNo);
if (AutoFillToolResult == MessageBoxResult.Yes)
// User directs application to the specified file
System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
// Application looks for specific file and removes unwanted data
string robotBackupPath = folderBrowser.SelectedPath;
string allDataPath = robotBackupPath + @"\RAPID\TASK1\SYSMOD\ALL_DATA.sys";
string[] tLines = File.ReadAllLines(allDataPath);
List<string> toolDataLines = new List<string>();
foreach (string tLine in tLines)
if (tLine.Contains("PERS tooldata") && !tLine.StartsWith("!"))
if (tLine.Contains("tToolChanger")) continue;
if (tLine.Contains("tPointer")) continue;
if (tLine.Contains("tHome")) continue;

foreach (string line in toolDataLines)
// Gets the name of the tool
ToolData toolData = GetToolNameFromLine(line);

// Puts the tool name in the DataGrid

private ToolData GetToolNameFromLine(string line)
// Removes white space at the beggining of line in txt file
ToolData tooldata = new ToolData();
string[] spaceSplit = line.Trim().Split(' ');
string values = spaceSplit[2];

// Gets Tool Name
int colonLocation = values.IndexOf(":");
tooldata.ToolName = values.Substring(0, colonLocation);
return tooldata;

Answer Source

If all the samples you'll have follow the same pattern, extracting those values does not seem difficult:

//First we get all the string after the :=
string tooldata = line.Substring(data.IndexOf(":=") + 2) ;

//Split the string by [        
string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries);
//the second and the third strings are what we are interested in
string xyzValue  = tooldataArray[1].Replace(']' ,' ');
string Q1234value = tooldataArray[2].Replace(']', ' ');

If after this you want to get the individual parameters, just splitting by , would do.


This would extract all the values you want to arrays of double:

string tooldata = data.Substring(data.IndexOf(":=") + 2) ;

string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries);
double[] xyzValue  = tooldataArray[1].Replace(']' ,' ').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
double[] Q1234value = tooldataArray[2].Replace(']', ' ').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download