我有一些字符串,我需要将其处理为小写并使用正则表达式将一些字符替换为空白。
Java 等价物是:
str.toLowerCase();
str.replaceAll("[^a-z]", "");
str.replaceAll("\\s", "");
在 c++03
约束下,不使用 Boost 或其他库,我如何才能在 C++ 中实现相同的功能?我运行的服务器的 g++ 版本是 4.8.5 20150623
。
小写很简单:
char asciiToLower(char c) {
if (c <= 'Z' && c >= 'A') {
return c - ('A' - 'a');
}
return c;
}
std::string manipulate(std::string str) {
for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
it = asciiToLower(it);
}
}
但是另外两个呢?
最佳答案
C++03 不支持正则表达式。这是在 C++11 中引入的。
因此,如果没有 (a) 外部库,或 (b) 自己编写正则表达式引擎,你不能。
但是,从 4.9 开始,GCC 在实验性 -std=c++0x
模式下支持正则表达式。因此,如果您可以参与其中,并且您的 GCC 足够新,也许这可以帮助您。
(不要误以为 GCC 4.8 支持它:it doesn't; it's lying。)
否则我建议你更新你的编译器。甚至 C++11 现在也过时了。
关于c++ - 如何在没有外部库的情况下使用 C++03 约束执行基于正则表达式的字符串操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582854/