【发布时间】:2019-10-07 13:48:30
【问题描述】:
我正在尝试过滤转义特殊字符并将其转换为小写的字符串。例如:"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;
}
【问题讨论】:
-
“ąžuolas!”的预期输出是什么? ?
-
应该输出
ąžuolas
标签: c++ regex unicode utf-8 c++14