如何对字符串使用通配符(匹配和替换)?

发布于 2024-12-15 04:14:19 字数 229 浏览 1 评论 0原文

我想搜索一些字母,包括 ? ,替换为 C++ 字符串中匹配的字母。

想像abcdefgh这样的词。我想找到一种算法来搜索输入 ?c 中任何被 ? 替换的字母,并找到 bc,但它也应该检查 ?e? 并找到 def

你有什么想法吗?

I want to search for a number of letters including ? replaced by a letter matched in a string in C++.

Think of a word like abcdefgh. I want to find an algorithm to search for an input ?c for any letter replaced by ?, and finds bc, but also it should also check for ?e? and find def.

Do you have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2024-12-22 04:14:19

使用 boost::regex 怎么样?或 std::regex 如果您使用的是启用 c++11 的编译器。

How about using boost::regex? or std::regex if you're using c++11 enabled compilers.

但可醉心 2024-12-22 04:14:19

如果您只想支持 ?,那非常简单:当您在模式中遇到 ? 时,只需向前跳过一个输入字节(或检查 isalpha,如果你真的只想匹配字母)。

编辑:假设更复杂的问题(从输入字符串中的任何位置开始查找匹配),您可以使用类似这样的代码:

#include <string>

size_t match(std::string const &pat, std::string const &target) { 

    if (pat.size() > target.size())
        return std::string::npos;

    size_t max = target.size()-pat.size()+1;

    for (size_t start =0; start < max; ++start) {
        size_t pos;
        for (pos=0; pos < pat.size(); ++pos)
            if (pat[pos] != '?' && pat[pos] != target[start+pos])
                break;
        if (pos == pat.size())
            return start;
    }
    return std::string::npos;
}

#ifdef TEST
#include <iostream>

int main() { 
    std::cout << match("??cd?", "aaaacdxyz") << "\n";
    std::cout << match("?bc", "abc") << "\n";
    std::cout << match("ab?", "abc") << "\n";
    std::cout << match("ab?", "xabc") << "\n";
    std::cout << match("?cd?", "cdx") << "\n";
    std::cout << match("??cd?", "aaaacd") << "\n";
    std::cout << match("??????", "abc") << "\n";
    return 0;
}

#endif

如果您只想根据整个模式是否与整个输入匹配来表示是/否,您做了几乎相同的事情,但是使用 != 而不是 > 进行初始测试,然后基本上删除外循环。

If you just want to support ?, that's pretty easy: when you encounter a ? in the pattern, just skip ahead over one byte of input (or check for isalpha, if you really meant you only want to match letters).

Edit: Assuming the more complex problem (finding a match starting at any position in the input string), you could use code something like this:

#include <string>

size_t match(std::string const &pat, std::string const &target) { 

    if (pat.size() > target.size())
        return std::string::npos;

    size_t max = target.size()-pat.size()+1;

    for (size_t start =0; start < max; ++start) {
        size_t pos;
        for (pos=0; pos < pat.size(); ++pos)
            if (pat[pos] != '?' && pat[pos] != target[start+pos])
                break;
        if (pos == pat.size())
            return start;
    }
    return std::string::npos;
}

#ifdef TEST
#include <iostream>

int main() { 
    std::cout << match("??cd?", "aaaacdxyz") << "\n";
    std::cout << match("?bc", "abc") << "\n";
    std::cout << match("ab?", "abc") << "\n";
    std::cout << match("ab?", "xabc") << "\n";
    std::cout << match("?cd?", "cdx") << "\n";
    std::cout << match("??cd?", "aaaacd") << "\n";
    std::cout << match("??????", "abc") << "\n";
    return 0;
}

#endif

If you only want to signal a yes/no based on whether the whole pattern matches the whole input, you do pretty much the same thing, but with the initial test for != instead of >, and then basically remove the outer loop.

旧伤还要旧人安 2024-12-22 04:14:19

或者,如果您坚持使用“通配符”形式,则您要搜索的术语是“glob”(至少在类 UNIX 系统上)。

以 c 为中心的 API 可以在类 Unix 系统上的 glob.h 中找到,并且由两个调用 globglobfree 组成。手册的3。

切换到完整的正则表达式将允许您使用更多 C++ 方法,如其他答案所示。

Or if you insist on "wildcards" in the form you exhibit the term you want to search for is "glob"s (at least on unix-like systems).

The c-centric API is to be found in glob.h on unix-like systems, and consists of two calls glob and globfree in section 3 of the manual.

Switching to full regular expressions will allow you to use a more c++ approach as shown in the other answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文