Reputation: 9898
I am trying to replace *
with whenever
\w*\w
pattern is found. Here is what I have
#include <string>
#include <iostream>
#include <regex>
using namespace std;
int main() {
string text = "Dear Customer, You have made a Debit Card purchase of INR962.00 on 26 Oct. Info.VPS*Brown House. Your Net Available Balance is INR 5,584.58.";
regex reg("[\w*\w]");
text = regex_replace(text, reg, " ");
cout << text << "\n";
}
But it replace the *
with and
w
with also.
Output of the above program is
Dear Customer, You have made a Debit Card purchase of INR962.00 on 26 Oct. Info.VPS Bro n House. Your Net Available Balance is INR 5,584.58.
Upvotes: 0
Views: 210
Reputation: 626920
Use
regex reg(R"(([a-zA-Z])\*(?=[a-zA-Z]))");
text = regex_replace(text, reg, "$1 ");
// => Dear Customer, You have made a Debit Card purchase of INR962.00 on 26 Oct. Info.VPS Brown House. Your Net Available Balance is INR 5,584.58.
See C++ online demo
The R"(([a-zA-Z])\*(?=[a-zA-Z]))"
is a raw string literal where \
is treated as a literal \
symbol, not an escaping symbol for entities like \n
or \r
.
The pattern ([a-zA-Z])\*(?=[a-zA-Z])
matches and captures an ASCII letter char (with ([a-zA-Z])
), then matches a *
(with \*
) and then requires (does not consume) an ASCII letter char (with (?=[a-zA-Z])
).
The $1
is the backreference to the value captured with the ([a-zA-Z])
group.
Upvotes: 2