9

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?

3
  • What exactly is not working? Commented Oct 28, 2015 at 6:37
  • I cannot specify the string with which I want to replace the expression that I found. In cpp I cannot use the function regex_replace() as i have mentioned in my question. Commented Oct 28, 2015 at 6:39
  • Then why are you giving regex_replace() as an example of what you are trying to do? Commented Oct 28, 2015 at 6:40

2 Answers 2

7

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

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

1 Comment

I am accepting this answer because my problem was I was not using R while defining my regular expression for which I was getting runtime error.
5

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)

7 Comments

Don't you need to escape those dollar signs, e.g. \\b\$<.+>\$\\b ?
@Tim, That depends on the grammar chosen, and in this case it seems to work fine for me.
Fair enough. I guess every language has its own particular way of doing regex.
@TimBiegeleisen, actually I've just tested both ways and there seems to be no difference, though the documentation says it should mean "end of line" if un-escaped. I'll add the note to the answer.
I would default to using standard regex conventions as a rule, though I don't doubt your C++ code ^ ^
|

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.