匹配不区分大小写的整个字符串
Matching a whole string with case insensitive
我正在寻找一个可以匹配整个单词的函数,例如:
std::string str1 = "I'm using firefox browser";
std::string str2 = "The quick brown fox.";
std::string str3 = "The quick brown fox jumps over the lazy dog.";
对于单词fox
,只有str2
和str3
应该匹配。因此,单词前后是否有句点(.)或逗号(,)这样的符号并不重要,它应该匹配,同时它还必须是不区分大小写的搜索。
我已经找到了很多方法来搜索不区分大小写的字符串,但我想知道一些匹配整个单词的方法。
我想推荐C++11的std::regex
。但是,对于g++4.8,它还不起作用。因此,我建议更换boost::regex
。
#include<iostream>
#include<string>
#include<algorithm>
#include<boost/regex.hpp>
int main()
{
std::vector <std::string> strs = {"I'm using firefox browser",
"The quick brown fox.",
"The quick brown Fox jumps over the lazy dog."};
for( auto s : strs ) {
std::cout << "n s: " << s << 'n';
if( boost::regex_search( s, boost::regex("\<fox\>", boost::regex::icase))) {
std::cout << "n Match: " << s << 'n';
}
}
return 0;
}
/*
Local Variables:
compile-command: "g++ --std=c++11 test.cc -lboost_regex -o ./test.exe && ./test.exe"
End:
*/
输出为:
s: I'm using firefox browser
s: The quick brown fox.
Match: the quick brown fox.
s: The quick brown Fox jumps over the lazy dog.
Match: the quick brown fox jumps over the lazy dog.
相关文章:
- 编写宏函数来更改字符串的大小写?
- 如何执行不区分大小写的字符串比较?
- 对字符串向量进行排序,但不区分大小写
- 如何创建一个程序来标识最长的子字符串回文,而不考虑字母大小写
- 哈希字符串更改大小写
- 在字符串的 boost::unordered_set 中使用 Lambda 函数 - 不区分大小写
- 开关大小写语句中函数中的字符串输入会导致无限循环和绕过输入
- 比较字符串大小写不区分的简单方法是什么?
- 使用 std::string 返回开关大小写块中的字符串常量
- 如何使字符串中的每个字母不区分大小写
- 不区分大小写的字符串
- 是否有函数/WinAPI 可以判断一个字符串是否以不区分大小写的语言方式以另一个字符串开头?
- 使用 map<string,int> 在开关大小写语句中使用字符串
- 处理从 std::istream 中提取字符串的空字符串大小写
- C++大小写中的类型转换常量字符串
- C++,检查两个字符串的不区分大小写相等
- 在字符串向量中查找字符串,不区分大小写的c++
- 如何按大小写顺序排列字符串(而不是按字典顺序排列)
- 内置C++函数,使两个字符串的比较不区分大小写
- 用于转换字符串大小写的函数