regular expression to get values out of a line of text

Posted on 2005-04-26
Last Modified: 2010-04-16
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"]);

I'm not sure why i can't get the last number.
Question by:vonster
    LVL 96

    Expert Comment

    by:Bob Learned
    Try this:


    Then, get the numbers from all the matches.

    LVL 17

    Accepted Solution

    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"]);
    LVL 17

    Expert Comment

    Output was:

    73 -12.759 28.666 41.476

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now