[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

regular expression to get values out of a line of text

I'm using regular expressions to get some data out of a text file.  For some reason i'm not getting all of the numbers i need.  Here is the line of text:

ATOM      2  CA  ALA 1  73     -12.759  28.666  41.476  1.00 39.97      1AFB 338

what I need is  73     -12.759  28.666  41.476
what i'm getting is 73     -12.759  28.666

Here is my code:
Regex regex3 = new Regex(@"ATOM\s+\d+\s+CA\s+\w+\s+1\s+(?<one>\S+)\s+(?<two>\S+)\s+(?<three>\S+)\s+(?<four>\S+).+", RegexOptions.Compiled);

while((input = reader.ReadLine()) != null)
{
  Match match3 = regex3.Match(input);
  if (match3.Success)
  {
    output.Write("{0} {1} {2} {3}",match2.Groups["one"], match2.Groups["two"], match2.Groups["three"], match2.Groups["four"]);
    output.WriteLine();
  }

I'm not sure why i can't get the last number.
0
vonster
Asked:
vonster
  • 2
1 Solution
 
Bob LearnedCommented:
Try this:

([\d.\-])+

Then, get the numbers from all the matches.

Bob
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Are you very sure it's not working (I know it sounds strange when people say that, but I tried the exact regex and it just works).

This is the code I used:

            string input = "ATOM      2  CA  ALA 1  73     -12.759  28.666  41.476  1.00 39.97      1AFB 338";
            Regex regex3 = new Regex(@"ATOM\s+\d+\s+CA\s+\w+\s+1\s+(?<one>\S+)\s+(?<two>\S+)\s+(?<three>\S+)\s+(?<four>\S+).+", RegexOptions.Compiled);

            Match match3 = regex3.Match(input);
            if (match3.Success)
            {
                  WL("{0} {1} {2} {3}",match3.Groups["one"], match3.Groups["two"], match3.Groups["three"], match3.Groups["four"]);
            }
            RL();
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Output was:

73 -12.759 28.666 41.476
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now