C++正则表达式笔记之wregex

遍历所有匹配

#include <iostream>
#include <regex>
using namespace std;
int main()
{
    wstring wstr = L"我是1994年出生的,我今年25岁了。";
    wsmatch wsm;
    wregex wre(L"[0-9]+");
    wsregex_iterator itr1(wstr.begin(), wstr.end(), wre);
    wsregex_iterator itr2;
    for (wsregex_iterator itr = itr1; itr != itr2; ++itr)
    {
        wcout << itr->str() << endl;
    }
    return 0;
}

在目标文本中进行搜索

#include <iostream>
#include <regex>
using namespace std;
int main()
{
    wstring text = L"百度搜索引擎https://www.baidu.com/^_^";
    wsmatch wsm;
    wregex wre(L"https?://(.+?)/");
    if (regex_search(text, wsm, wre))
    {
        wcout << wsm.str(1) << endl;
    }
    const wchar_t *str = L"百度搜索引擎https://www.baidu.com/^_^";
    wcmatch wcm;
    if (regex_search(str, wcm, wre))
    {
        wcout << wsm[1] << endl;
    }
    return 0;
}

完全匹配

#include <iostream>
#include <regex>
using namespace std;
int main()
{
    wstring text = L"long long ago";
    wstring text2 = L"long long";
    wregex wre(L".+ng");
    wcout << boolalpha << regex_match(text, wre) << endl;
    wcout << regex_match(text2, wre) << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/buyishi/p/10440944.html