Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:45:54+00:00 2026-05-29T22:45:54+00:00

I am trying to parse a text file the contains numeric data. I have

  • 0

I am trying to parse a text file the contains numeric data. I have a lot of lines that look like

129.3 72.7 121.6 173.6 203.3 120.7 40.5 79.2 94.0 123.2 165.8 178.8 135.5 78.5 66.2

but the length of the lines vary. Each line is also preceded by a few spaces.
I would like to use regular expressions to parse the line and place each number into an array that I can then manipulate later.

Using

std::getline(is, line);

std::tr1::regex rx("[0-9-\.]+");
std::tr1::cmatch res;
std::tr1::regex_search(line.c_str(), res, rx);

only matches the first number. If instead I use line anchors such as

"^[0-9-\.]+$" 
"^[0-9-\.]+"

I get no matches and

"[0-9-\.]+$"

just matches the last number. So I am probably doing something wrong. Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:45:55+00:00Added an answer on May 29, 2026 at 10:45 pm

    Um, pseudocode

     for str in strtok(input string)
         vector[index] = convert str to float
    

    Here’s an example using lots of stream magic: Split a string in C++?

    Here’s an example using a vector:
    Splitting a string by whitespace in c++

    But plain old strtok is probably easiest:
    http://www.cplusplus.com/reference/clibrary/cstring/strtok/

    in which case you’ll get something like

    Vector flts = // create it 
    for(int ix=0, char * cp; cp = strtok(str," "); ix++){
        flts[ix] = atof(cp);
    }
    

    Now, that’s very C like because I’m out of practice for C++, but the key point here is that by trying to use regex, you make it overcomplicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which I'm trying to parse. The file looks like
I am trying to parse a text file that contains a variable number of
In Java, I am trying to parse an HTML file that contains complex text
I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
I have a file log that I would like to parse and am having
I'm trying to parse a text file that has a heading and the body.
I’m trying to parse text from a file that comes in a pseudo XML
I'm trying to parse text-based file attachments (txt, doc, etc...). However, I can't seem
I am trying to parse some text and diagram it, like you would a
I have a text file that lists the names of a large number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.