2

I am trying to remove all characters that are not digit, dot (.), plus/minus sign (+/-) with empty character/string for float conversion.
When I pass my string through regex_replace function I am returned an empty string.
I belive something is wrong with my regex expression std::regex reg_exp("\\D|[^+-.]")

Code

#include <iostream>
#include <regex>

int main()
{
    std::string temporary_recieve_data = "    S S   +456.789 tg\r\n";
    std::string::size_type  sz;
    const std::regex        reg_exp("\\D|[^+-.]");   // matches not digit, decimal point (.), plus sign, minus sign
    std::string             numeric_string = std::regex_replace(temporary_recieve_data, reg_exp, ""); //replace the character that are not digit, dot (.), plus-minus sign (+,-) with empty character/string for float conversion
    std::cout << "Numeric String : " << numeric_string << std::endl;
    if (numeric_string.empty())
    {
        return 0;
    }
    float           data_value = std::stof(numeric_string, &sz);
    std::cout << "Float Value : " << data_value << std::endl;
    return 0;
}

I have been trying to evaluate my regex expression on regex101.com for past 2 days but I am unable to figure out where I am wrong with my regular expression. When I just put \D, the editor substitutes non-digit character properly but soon as I add or condition | for not dot . or plus + or minus - sign the editor returns empty string.

3
  • 1
    I recommend you use something like regex101.com to test your reqular expression first. Commented Dec 3, 2022 at 1:53
  • @Someprogrammerdude I have been trying to evaluate my regex expression on regex101.com for past 2 days but I am unable to figure out where I am wrong with my regular expression. When I just put \D, the editor substitutes non-digit character properly but soon as I add or condition for dot (.) or plus or minus sign the editor returns empty string. Commented Dec 3, 2022 at 1:59
  • This approach is prone to problems (or at least results many find surprising). For example, I'd find it somewhat surprising when 1abd2 converted to 12. Fine to ignore the abc, but probably not usually so good to take things that had previously been separate, and mash them together into a single number. Commented Dec 3, 2022 at 4:14

1 Answer 1

3

The string is empty because your regex matches each character.

  • \D already matches every character that is not a digit.
    So plus, hyphen and the period thus far are consumed.
    And digits get consumed by the negated class: [^+-.]
  • Further the hyphen indicates a range inside a character class.
    Either escape it or put it at the start or end of the char-class.
    (funnily the used range +-. 43-46 even contained a hyphen)

Remove the alternation with \D and put \d into the negated class:

[^\d.+-]+

See this demo at regex101 (attaching + for one or more is efficient)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.