如何获取未知数量的正则表达式匹配项

How to obtain an unknown number of regex matches?

本文关键字:正则表达式 未知数 何获取 获取      更新时间:2023-10-16

我试图在一个字符串中找到几个数字的位置。我只能得到最后一个,或以前指定的位数:

#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string s("aaabbbccd123456eeffgg");
    std::smatch match;
    std::regex braced_regex("(\w+)(\d{2,})(\w+)");
    std::regex plus_regex("(\w+)(\d+)(\w+)");
    auto printer = [](auto& match) {
            std::ssub_match sub(match);
            std::string match_substring(sub.str());
            std::cout <<  match_substring << 'n';
    };
    std::regex_match(s, match, braced_regex);
    std::cout << "Number of braced matches: " << match.size() << 'n';  
    std::for_each(match.begin(), match.end(), printer);
    std::regex_match(s, match, plus_regex);
    std::cout << "Number of plus matches: " << match.size() << 'n';  
    std::for_each(match.begin(), match.end(), printer);
    return 0;
}

结果:

Number of braced matches: 4
aaabbbccd123456eeffgg
aaabbbccd1234
56
eeffgg
Number of plus matches: 4
aaabbbccd123456eeffgg
aaabbbccd12345
6
eeffgg

如何从提供的字符串中获取整数序列,即123456?

([a-zA-Z]+)(\d{2,})([a-zA-Z]+)

你可以试试这个。w === [a-zA-Z0-9_] .所以w+会匹配它所能达到的最大目标。所以它只允许d{2,} 2。

(\w+?)(\d{2,})(\w+)

让第一个w不贪婪。观看现场演示。

我认为

问题是数字被认为是单词部分并与w匹配。我很想使用D意思不是数字

#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string s("aaabbbccd123456eeffgg");
    std::smatch match;
    std::regex plus_regex("(\D+)(\d+)(\D+)");
    auto printer = [](auto& match) {
            std::ssub_match sub(match);
            std::string match_substring(sub.str());
            std::cout <<  match_substring << 'n';
    };
    std::regex_match(s, match, plus_regex);
    std::cout << "Number of plus matches: " << match.size() << 'n';
    std::for_each(match.begin(), match.end(), printer);
    return 0;
}

输出:

Number of plus matches: 4
aaabbbccd123456eeffgg
aaabbbccd
123456
eeffgg

另一种可能性(取决于你想要的)是使用std::regex_search()它不会尝试匹配整个字符串,但允许您匹配中间的元素:

#include <iostream>
#include <regex>
#include <string>
int main()
{
    std::string s("aaabbbccd123456eeffgg");
    std::smatch match;
    std::regex braced_regex("\d{2,}"); // just the numbers
    auto printer = [](auto& match) {
            std::ssub_match sub(match);
            std::string match_substring(sub.str());
            std::cout <<  match_substring << 'n';
    };
    std::regex_search(s, match, braced_regex); // NOTE: regex_search()!
    std::cout << "Number of braced matches: " << match.size() << 'n';
    std::for_each(match.begin(), match.end(), printer);
}

输出:

Number of braced matches: 1
123456

在:

(\w+)(\d{2,})(\w+)

\w+匹配任何单词字符 [a-zA-Z0-9_],因此它也匹配 1234

将整数更改\w与 [a-zA-Z_] 匹配,因此您将拥有:

std::regex braced_regex("([a-zA-Z_]+)(\d{2,})(\w+)");