mxdg
mxdg

Reputation: 314

matching exact substring using std::regex in C++

I am trying to make a regex to match an exact substring so that I can remove it from a string.

std::string str = "{abc}slkj skdjv{abc}nei slkjdf";
std::regex reg("{abc}");
str = std::regex_replace(str, reg, "");

I tried this regex on Regexr and it works (it matches the {abc} like I want it to). However, the above code seems to run into an infinite loop.

The reason I want to use a regex is because I am also using it to remove other things that aren't just string matching, so I am not looking for straight string operations like the answer given here.

What should the regex string actually be to match that specific string?

Upvotes: 2

Views: 2739

Answers (1)

Mike Precup
Mike Precup

Reputation: 4218

If you want to actually match {abc}, you need to use the regex \{abc\}. {} are special tokens in regex, and so they must be escaped.

Upvotes: 3

Related Questions