Gayathri Jeyaram
Gayathri Jeyaram

Reputation: 33

combining regex in c++

I want to replace everything in a line except for alphabets, numbers and periods with whitespaces in c++.

Could anyone please give me a regex in c++ that I can use ?

I was using [^[:alnum:]] till now but that works only for alpha and numeric.

Thank you !

Upvotes: 0

Views: 262

Answers (2)

Pete Becker
Pete Becker

Reputation: 76315

std::replace_if(line.begin(), line.end(),
    [](char ch){ return !isalnum(ch) && ch != '.'; }, ' ');

No need to argue about escapes.

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521437

I'm not a C++ person, but you can try adding dots to the regex:

[^[:alnum:].]

An alternative:

[^a-zA-Z0-9.]

Upvotes: 2

Related Questions