首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++字符串中查找完全匹配的单词

在C++字符串中查找完全匹配的单词
EN

Stack Overflow用户
提问于 2021-09-25 11:06:01
回答 4查看 267关注 0票数 0

我有以下字符串:

代码语言:javascript
复制
std::string s1 = "IAmLookingForAwordU and I am the rest of the phrase";
std::string keyWord = "IAmLookingForAword";

我想知道在s1中keyWord是否完全匹配

我使用:

代码语言:javascript
复制
   if ( s1.find(keyWord) != std::string::npos )
    {
        std::cout << "Found " << keyWord << std::endl;
    }

但是find函数捕获IAmLookingForAwordU中的IAmLookingForAword,并且if语句被设置为true。但是,我只想捕获与我正在查找的keyWork完全匹配的内容。

有什么方法可以用C++字符串做到这一点吗?

EN

回答 4

Stack Overflow用户

发布于 2021-09-25 11:15:22

如果你想继续使用std::string::find,你可以测试单词前后的字符是否超出了字符串、标点符号或空格的界限:

代码语言:javascript
复制
bool find_word(const std::string& haystack,const std::string& needle){
    auto index = haystack.find(needle);
    if (index == std::string::npos) return false;

    auto not_part_of_word = [&](int index){ 
        if (index < 0 || index >= haystack.size()) return true;
        if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true;
        return false;
    };
    return not_part_of_word(index-1) && not_part_of_word(index+needle.size());
}
 

int main()
{
    std::cout << find_word("test","test") << "\n";    // 1
    std::cout << find_word(" test ","test") << "\n";  // 1
    std::cout << find_word("AtestA","test") << "\n";  // 0
    std::cout << find_word("testA","test") << "\n";   // 0
    std::cout << find_word("Atest","test") << "\n";   // 0
}
票数 1
EN

Stack Overflow用户

发布于 2021-09-25 11:20:28

函数在IAmLookingForAwordU中捕获IAmLookingForAword,并将if语句设置为true。但是,我只想捕获与我正在查找的keyWork完全匹配的内容。

有什么方法可以用C++字符串做到这一点吗?

您可以为此定义一个助手函数:

代码语言:javascript
复制
#include <string>
#include <cctype>
// ...

bool has_word(std::string const& s, std::string const& key_word) {
    auto const found_at = s.find(key_word);
    return found_at != std::string::npos
        && (!found_at || (found_at && !isalpha(s[found_at - 1])))
        && found_at <= s.size() - key_word.size() && !isalpha(s[found_at + key_word.size()]);
}

然后像这样使用它:

代码语言:javascript
复制
if (has_word(s1, keyWord))
    std::cout << "Found " << keyWord << std::endl;
票数 0
EN

Stack Overflow用户

发布于 2021-09-25 11:25:57

一种想法是使用正则表达式。这里有一个简单的例子。正则表达式在单词"exact“的两侧使用\b。在正则表达式中,\b意味着它应该只在单词边界(例如空格或标点符号)处匹配。这个正则表达式将只匹配单词"exact“,而不匹配单词”exact“。注:在正则表达式中使用原始字符串通常更容易,因为反斜杠字符对于C++字符串和正则表达式都有特殊的含义。

代码语言:javascript
复制
#include <string>
#include <regex>
#include <iostream>

int main() {
    std::regex re(R"(\bexact\b)");
    std::smatch m;

    std::string string1 = "Does this match exactly?";
    std::string string2 = "Does this match with exact precision?";

    if (std::regex_search(string1, m, re))
    {
        // this shouldn't print
        std::cout << "It matches string1" << std::endl;
    }

    if (std::regex_search(string2, m, re))
    {
        // this should print
        std::cout << "It matches string2" << std::endl;
    }

    return 0;
}

如果您正在搜索的单词是可变的(即,您要查找的单词每次都是不同的),那么使用正则表达式就会变得复杂得多,因为您必须确保正确地验证输入,以及正确地转义正则表达式中具有特殊含义的字符。因此,我可能会选择其他解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69325536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档