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.
CodePudding user response:
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)
CodePudding user response:
The regex_replace function in C is used to search for a regular expression in a string and replace it with a different string. If the function is returning an empty string, it could be because the regular expression being searched for was not found in the input string, or because there was an error in the regular expression itself.
Here is an example of how the regex_replace function might be used:
#include <iostream>
#include <regex>
using namespace std;
int main() {
string input = "Hello, world!";
regex pattern("world");
string result = regex_replace(input, pattern, "there");
cout << result << endl; // Outputs "Hello, there!"
return 0;
}
If the regex_replace function is returning an empty string in your code, you can try the following steps to troubleshoot the issue:
Verify that the input string actually contains the text that you are searching for with the regular expression. You can do this by printing the input string to the console or using a debugger to inspect its value.
Check the regular expression itself to make sure that it is valid and matches the text that you are trying to replace. Regular expressions can be tricky to get right, so it's worth double-checking to make sure there are no syntax errors or other issues with the pattern.
If the regular expression is correct and the input string contains the text you are searching for, it's possible that there is an error in your code that is causing the regex_replace function to return an empty string. In this case, you can try using a debugger to step through your code and see what is happening at the point where the regex_replace function is called. This can help you identify the root cause of the problem and fix it.