RegEx Replaces Duplicate Character Sequences but Removes Dots and Commas Entirely (but Shouldn't)

30 Views Asked by At

I'm trying to write a regex for replacing sequential occurrence of specified characters with single ones. I used a backreference and character class for that. However, some of the characters (. and ,, in my case) just get completely removed (and I cannot figure out what I've missed).

Question: Why does it work that way?

Note: I'm using C++20.

std::string s{ "...?12 :: 54  ! !! ..,,,- ---" };
const std::regex re("([.,\\-:!?]){2,}");
s = std::regex_replace(s, re, "$1");

I expected to get .?12 : 54 ! ! .,- -, but, instead, I get ?12 : 54 ! ! - -. Escaping . and , didn't help either.

0

There are 0 best solutions below