I am using the following regex
JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)*
for the following data type:
JOINTS DISPL.-X DISPL.-Y ROTATION 1 0.000000E+00 0.975415E+01 0.616921E+01 2 0.000000E+00 0.000000E+00 0.000000E+00
The idea is to extract two groups, each of which contains a string (starting with Joint Number, 1, 2, etc.). C # code is as follows:
string jointPattern = @"JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)*"; MatchCollection mc = Regex.Matches(outFileSection, jointPattern ); foreach (Capture c in mc[0].Captures) { JointOutput j = new JointOutput(); string[] vals = c.Value.Split(); j.Joint = int.Parse(vals[0]) - 1; j.XDisplacement = float.Parse(vals[1]); j.YDisplacement = float.Parse(vals[2]); j.Rotation = float.Parse(vals[3]); joints.Add(j); }
However, this does not work: instead of returning two captured groups (inside the group), it returns one group: the entire block, including the column headings. Why is this happening? Does C # deal with non-captured groups in different ways?
Finally, RegExes the best way to do this? (I really feel that I have two problems now.)
ian93
source share