I am writing a piece of software, and It require me to handle data I get from a webpage with libcurl. When I get the data, for some reason it has extra line breaks in it. I need to figure out a way to only allow letters, numbers, and spaces. And remove everything else, including line breaks. Is there any easy way to do this? Thanks.
To remove all non-alphanumeric characters from a string, call the replace() method, passing it a regular expression that matches all non-alphanumeric characters as the first parameter and an empty string as the second. The replace method returns a new string with all matches replaced.
Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.
Write a function that takes a char
and returns true
if you want to remove that character or false
if you want to keep it:
bool my_predicate(char c);
Then use the std::remove_if
algorithm to remove the unwanted characters from the string:
std::string s = "my data"; s.erase(std::remove_if(s.begin(), s.end(), my_predicate), s.end());
Depending on your requirements, you may be able to use one of the Standard Library predicates, like std::isalnum
, instead of writing your own predicate (you said you needed to match alphanumeric characters and spaces, so perhaps this doesn't exactly fit what you need).
If you want to use the Standard Library std::isalnum
function, you will need a cast to disambiguate between the std::isalnum
function in the C Standard Library header <cctype>
(which is the one you want to use) and the std::isalnum
in the C++ Standard Library header <locale>
(which is not the one you want to use, unless you want to perform locale-specific string processing):
s.erase(std::remove_if(s.begin(), s.end(), (int(*)(int))std::isalnum), s.end());
This works equally well with any of the sequence containers (including std::string
, std::vector
and std::deque
). This idiom is commonly referred to as the "erase/remove" idiom. The std::remove_if
algorithm will also work with ordinary arrays. The std::remove_if
makes only a single pass over the sequence, so it has linear time complexity.
Previous uses of std::isalnum
won't compile with std::ptr_fun
without passing the unary argument is requires, hence this solution with a lambda function should encapsulate the correct answer:
s.erase(std::remove_if(s.begin(), s.end(), []( auto const& c ) -> bool { return !std::isalnum(c); } ), s.end());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With