0

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 !

7
  • except for alphabets, numbers and periods with whitespaces ... can you show us an example? Commented Oct 11, 2016 at 1:31
  • 1
    I think you just need to add a period to your character class: [^[:alnum:].] Commented Oct 11, 2016 at 1:33
  • @Galik Careful, that dot needs to be escaped AFAIK. Commented Oct 11, 2016 at 1:35
  • @TimBiegeleisen I can't check it at the moment but I thought inside [] only [, ], - and `\` needed escaping. Commented Oct 11, 2016 at 1:42
  • @Galik I think you're right. Commented Oct 11, 2016 at 1:50

2 Answers 2

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

No need to argue about escapes.

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

Comments

2

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

[^[:alnum:].]

An alternative:

[^a-zA-Z0-9.]

1 Comment

I think that Gayathri Jeyaram want to replace all that isn't an (alphanumeric or a dot) with white-space (probably a space), I'm not sure you need to include \s.

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.