c++ - 使用 utf8 格式的正则表达式过滤字符串

标签 c++ regex unicode utf-8 c++14

我正在尝试过滤转义特殊字符并将其转换为小写的字符串。例如:"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}*)+)*)

Demo

Source

关于c++ - 使用 utf8 格式的正则表达式过滤字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233667/

相关文章:

javascript - 如何检查 JavaScript 中用户浏览器是否不支持字符?

c++ - C++中正态分布的随机数

c++ - clang/g++ 与友元函数的区别

javascript - 替换 Div 中的所有管道字符

python - Tkinter 和 32 位 Unicode 复制 - 任何修复?

c++ - (C++) 在 Windows CMD 中显示阴影 block (↓、▒、░)

c++ - 一种在 C++ 中读取二进制文件的 Kosher 方法

c++ - Eclipse/MinGW/CDT/GDB 和调试问题

python - 基于正则表达式过滤数据框

html - 如何匹配任一正则表达式?