I am trying to use std::regex
to truncate on the first non-printable character.
I tried
std::string ExtractPrintableString(const std::string& message) {
std::regex trim_nonprintable_regex("([[:print:]] ).*")
std::smatch matched_message;
std::regex_search(message, matched_message, trim_nonprintable_regex);
return matched_message[1].str()
}
But I am not getting the expected result.
Ex:
If I pass "\r\t\r\t\r\t\ruessw7cr9jhmdiy"
it should return empty string.
If I pass "asd\r\tdfvdfv"
then it should return asd
only.
CodePudding user response:
You need to 1) match at the start of string by adding a ^
at the start and 2) by replacing the ([[:print:]] ).*
pattern with just [[:print:]]*
:
std::string ExtractPrintableString(const std::string& message) {
std::regex trim_nonprintable_regex("^[[:print:]]*");
std::smatch matched_message;
std::regex_search(message, matched_message, trim_nonprintable_regex);
return matched_message[0].str();
}
Note that in this case, there is no capturing group, so you need to return matched_message[0].str()
.
See the online C demo.
Pattern details:
^
- start of string[[:print:]]*
- zero or more printable chars (that are only matched at the start of string due to^
).
CodePudding user response:
In std::regex trim_nonprintable_regex("([[:print:]] ).*")
the
means that at least 1 character. So it will never lead to an empty string. Change it to *
ant it will work. Also you forgot some ;
-s
std::string ExtractPrintableString(const std::string& message) {
std::regex trim_nonprintable_regex("([[:print:]]*).*");
std::smatch matched_message;
std::regex_search(message, matched_message, trim_nonprintable_regex);
return matched_message[1].str();
}