Find exact substr in string

I have a text file containing the following text

License = "123456"

GeneralLicense = "56475655"

I want to find Licenseas well for GeneralLicense.

while (getline(FileStream, CurrentReadLine))
{

    if (CurrentReadLine.find("License") != std::string::npos)
    {
        std::cout << "License Line: " << CurrentReadLine;
    }
    if (CurrentReadLine.find("GeneralLicense") != std::string::npos)
    {
        std::cout << "General License Line: " << CurrentReadLine;
    }
}

Since the word is Licensealso present in the word GeneralLicense, therefore if-statement, the line if (CurrentReadLine.find("License") != std::string::npos)becomes true twice.

How can I indicate that I want to find the exact substring?

UPDATE: I can reverse the order mentioned in some answers, or check if Licensethe index is zero. But is there something ROBOUST (flag or something else) that we can describe to find an exact match ( . Something similar to most editors, for example, MS Word, etc. ).

+4
6
while (getline(FileStream, CurrentReadLine))
{
    if (CurrentReadLine.find("GeneralLicense") != std::string::npos)
    {
        std::cout << "General License Line: " << CurrentReadLine;
    }
    else if (CurrentReadLine.find("License") != std::string::npos)
    {
        std::cout << "License Line: " << CurrentReadLine;
    }
}
+6

ROBUST- :

#include <regex>

while (getline(FileStream, CurrentReadLine))
{
    if(std::regex_match(CurrentReadLine,
        std::regex(".*\\bLicense\\b.*=.*")))
    {
        std::cout << "License Line: " << CurrentReadLine << std::endl;
    }
    if(std::regex_match(CurrentReadLine,
        std::regex(".*\\bGeneralLicense\\b.*=.*")))
    {
        std::cout << "General License Line: " << CurrentReadLine << std::endl;
    }
}

\b .

. * " , "

EDIT: regex_search regex_match , . * , :

#include <regex>

while (getline(FileStream, CurrentReadLine))
{
    if(std::regex_search(CurrentReadLine, std::regex("\\bLicense\\b"))) 
    {
        std::cout << "License Line: " << CurrentReadLine << std::endl;
    }
    if(std::regex_search(CurrentReadLine, std::regex("\\bGeneralLicense\\b")))
    {
        std::cout << "General License Line: " << CurrentReadLine << std::endl;
    }
}

, , , . , regex_match , .

+1

, , , , , - :

bool findAtWordBoundary(const std::string& line, const std::string& search) {
    size_t pos = line.find(search);
    return (pos != std::string::npos) && (pos== 0 || isspace(line[pos-1]));
}

ROBUST ( - ), ?

find . , , . std::string " ", , .

0

, , .

- :

// find the largest matching element from the set and return it
std::string find_one_of(std::set<std::string, std::greater<std::string>> const& tests, std::string const& s)
{
    for(auto const& test: tests)
        if(s.find(test) != std::string::npos)
            return test;
    return {};
}

int main()
{
    std::string text = "abcdef";

    auto found = find_one_of({"a", "abc", "ab"}, text);

    std::cout << "found: " << found << '\n'; // prints "abc"
}
0

pos 0, ,

if (CurrentReadLine.substr( 0, 7 ) == "License")
0

You can tokenize your string and do a full comparison with your search key and tokens

Example:

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

auto tokenizer(const std::string& line)
{
    std::vector<std::string> results;
    std::istringstream ss(line);
    std::string s;
    while(std::getline(ss, s, ' '))
        results.push_back(s);
    return results;
}

auto compare(const std::vector<std::string>& tokens, const std::string& key)
{
    for (auto&& i : tokens)
        if ( i == key )
            return true;
    return false;
}

int main()
{
    std::string x = "License = \"12345\"";
    auto token = tokenizer(x);
    std::cout << compare(token, "License") << std::endl;
    std::cout << compare(token, "GeneralLicense") << std::endl;
}
0
source

Source: https://habr.com/ru/post/1677552/


All Articles