kishoredbn
kishoredbn

Reputation: 2087

How to find and replace using std::regex_replace() function in cpp

I am working on a project where I need to find words starting with $< and ending with >$ and replace with it with a word stored in a variable.

Example

string a ="hello";
string b = "Fellow $<world>$, full of $<smart>$ people"
std::cout<<std::regex_replace(b, "\\b($<)([^ ]*)(>$)\\b", a); //should print "Fellow hello, full of hello people"

but seems like this is not possible directly.

How can I work around this?

Upvotes: 9

Views: 26286

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

Your code is fine with the exception of 2 points:

  • Regex - you have unescaped $ that means end of string, \b word boundary before and after $ that requires a word character to appear right next to the $ symbol.
  • There is no signature for regex_replace like the one you used.

So, the correct regex is

\$<[^<>]*>\$

The \$ matches a literal $, then follows a literal <, then 0 or more characters other than < and > up to the literal >$.

In C++, you can use raw strings (R"()") to declare regex objects, it will relieve the pain of escaping metacharacters twice.

See IDEONE demo:

string a ="hello";
string b = "Fellow $<world>$, full of $<smart>$ people";
std::cout<<std::regex_replace(b, std::regex(R"(\$<[^<>]*>\$)"), a);

Output: Fellow hello, full of hello people

Upvotes: 7

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

There are multiple problems here. First, regex_replace takes a basic_regex as the second parameter. Second, it doesn't perform the replace in-place, but returns a new string. Finally, you have some unnecessary parenthesis in your regular expression. So your code should look like this:

string input = "well, $<hello>$ there!";
std::regex reg("\\$<.+>\\$");
// prints "well, fellow there!":
std::cout << '\n' << std::regex_replace(input, reg, "fellow") << '\n';

Note that word boundary check (\\b) is not going to work here because the start and end characters of the sequence are dollar signs, and \\b marks word boundary, which means either

  • The beginning of a word (current character is a letter, digit, or underscore, and the previous character is not)
  • The end of a word (current character is not a letter, digit, or underscore, and the previous character is one of those)

Upvotes: 4

Related Questions