1

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?

9
  • Could you please clarify the problem? What are ptap, name and alias_name here? Commented Jun 20, 2022 at 14:11
  • "ptap" is a string it should be in quotes, like this: std::string name = "ptap"; Commented Jun 20, 2022 at 15:56
  • These are the names of the layers that make up a small part of a large project. If you find "ptap" in the text, you should replace it with the words alias_name. If you come across the word "ptap.power" or another "." containing, that word remains the same. Thanks for the feedback) Commented Jun 20, 2022 at 16:05
  • Please provide a valid test case to see what problem you have got. Commented Jun 20, 2022 at 17:56
  • sorry i could not use boost with online compiler but my main code will be written in boost Commented Jun 20, 2022 at 19:08

1 Answer 1

1

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(?!\\.)";
Sign up to request clarification or add additional context in comments.

5 Comments

Well, thank you very much for the previous answers. They worked very well in the previous cases but I have another problem.
@AzatZaroyan What is the problem?
What to do if in the previous case there is another character instead of a period, for example ptap:power, I need to replace it if it is ptap (it is not necessary at the beginning of the line, at the end or in the middle position), and in other cases if ptap: power, power:ptap, ptap-power will not be replaced
@AzatZaroyan You are not providing exact pattern requirements. If you just provide examples, I can provide a pattern - e.g. \b(?<![.:])ptap\b(?![.-]|:\s) (see demo) - but it won't work once you come back and say "oh, I have more examples...). Please formulate the rules for matching, update the question and let me know,
I just brought the examples to make my question clearer, I need it not to be distorted after the name in case of any character if such a case is possible or replace only if there are no extra characters after the name.)

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.