Thread: Regex_replace numbers in .txt file

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Regex_replace numbers in .txt file

    I want to regex_replace all numbers in a .txt file but for numbers with decimals the period separator remains:
    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <regex>
    
    
    int main()
    {
        std::ifstream inFile{"F:\\test.txt"};
        std::regex re{"\\b[0-9]+"};
         if (inFile)
        {
            std::string line{};
            while (getline(inFile, line))
            {
                line = std::regex_replace(line, re, "");
                std::cout << line;
            }
        }
    }
    Sample File
    The National Bureau of Statistic's official Purchasing Managers' Index (PMI) came in at 51.2 higher than the 51.0 expected and even with 51.2 in April, Reuters reported. China's steel PMI rose to 54.8 in May from 49.1 in April. Meanwhile, China's official services PMI rose to 54.5 in May from 54.0 in April.


    ps: there might be other ways of accomplishing this task but I'd like to use regex in this instance. Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you try adding the decimal point to the regex expression?

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Hello Jim - a well-placed query is often as powerful as providing a complete answer, so thanks for your perspicacious line of questioning. The following works but I'm using two regex objects, the second to remove the additional whitespace. Is there any way they can be combined into one?
    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <regex>
    
    
    int main()
    {
        std::ifstream inFile{"F:\\test.txt"};
        std::regex re{"\\b[0-9]+ | \\b[0-9]+(.)[0-9]+"};
        // numbers without decimals - regex separator - numbers with decimals 
        std::regex re1{"[\\s]{2}"};
        // preceding atom has exactly two consecutive whitespaces
         if (inFile)
        {
            std::string line{};
            while (getline(inFile, line))
            {
                line = std::regex_replace(line, re, "");
                line = std::regex_replace(line, re1, " ");
                std::cout << line;
            }
        }
    }
    Sample File (now contains a number without decimals as well for testing the regex)
    The National Bureau 45 of Statistic's official Purchasing Managers' Index (PMI) came in at 51.2 higher than the 51.0 expected and even with 51.2 in April, Reuters reported. China's steel PMI rose to 54.8 in May from 49.1 in April. Meanwhile, China's official services PMI rose to 54.5 in May from 54.0 in April.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The following works but I'm using two regex objects, the second to remove the additional whitespace.
    What additional whitespace, located exactly where?

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    The additional ws appears after the first regex object has executed where the previous digits used to be - one ws each from either side of the word boundary

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    one ws each from either side of the word boundary
    After commenting out the second regex lines I don't see any extra spaces in the line.

    Also using
    Code:
    std::regex re{"\\b[0-9]+."};
    seems to remove the numbers, with or without decimal places so are you sure you need the rest of the conditions in your regex?


    Jim

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Perfect Jim, many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  4. Reading numbers from a file, how can I do it?
    By angeloulivieri in forum C Programming
    Replies: 2
    Last Post: 06-11-2010, 05:11 AM
  5. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM

Tags for this Thread