我正在尝试过滤转义特殊字符并将其转换为小写的字符串。例如:"Good morning!"
转换为 good morning
。
我一次将一个字符串传递给我的函数。
我成功地过滤了我的英语字符串,但在传递母语字符串时遇到了问题。
如果我想包含所有 utf-8 字符,我应该使用什么类型的正则表达式过滤器字符串?
#include <string>
#include <iostream>
#include <regex>
#include <algorithm>
std::string process(std::string s) {
std::string st;
std::regex r(R"([^\W_]+(?:['_-][^\W_]+)*)");
std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
std::smatch m = *i;
st = m.str();
std::transform(st.begin(), st.end(), st.begin(), ::tolower);
return st;
}
int main() {
std::string st = "ąžuolas!";
std::cout << process(st) << std::endl; // <- gives: uolas
return 0;
}
最佳答案
您可以使用正则表达式 \p{L}\p{M}*
匹配任何 unicode“字母”字符。
因此,完整的正则表达式将是:
((?:\p{L}\p{M}*)+(?:['_-](?:\p{L}\p{M}*)+)*)
关于c++ - 使用 utf8 格式的正则表达式过滤字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233667/