Currently I have a problem with boost :: regex, I need to find the appropriate word և replace. with the corresponding word. My code now looks like this.
std::string name = ptap;
std::string name_regex = "\\b" name "\\b";
boost::regex reg(name_regex);
checks_str = boost::regex_replace( checks_str, reg, alias_name );
There are words in the file that look like "ptap.power" because regex reads the dot as any character. The initial part of this word (ptap) changes, which I do not need. How to fix this?
CodePudding user response:
You can prevent a match if there is a dot after a word:
#include <boost/regex.hpp>
#include <string>
#include <iostream>
int main()
{
std::string str = "ptap ptap.power";
std::string name = "ptap";
std::string rep = "pplug";
std::string regex_name = "\\b" name "\\b(?!\\.)";
boost::regex reg(regex_name);
str = boost::regex_replace(str, reg, rep);
std::cout << str << std::endl;
return 0;
}
See the C demo.
The (?!\.)
is a negative lookahead that fails the match if there is a .
char immediately to the right of the current location.
To avoid matching the keyword if there is a dot before, add a negative lookbehind:
std::string regex_name = "\\b(?<!\\.)" name "\\b(?!\\.)";