Is there a way to repeat the capture an arbitrary number of times in a regex?

I am using C ++ tr1 :: regex with ECMA regex grammar. What I'm trying to do is to parse the title and return the values ​​associated with each element in the title.


-Testing some text
-Numbers 1 2 5
-MoreStuff some more text
-Numbers 1 10


What I would like to do is find all "-Numbers" strings and put each number into its own result with one regex. As you can see, "-Numbers" strings can have any number of values ​​per string. Currently I am just looking for "-Numbers ([\ s0-9] +)" and then symbolizing this result. I was just wondering if there is a way to find and highlight the results in a single regex.


source to share

3 answers

No no.



I was about to ask this same question and I found a solution.

Let's say you have an arbitrary number of words that you want to capture.

"there are four lights"


"Captain Picard is the bomb"

You might think that the solution is:



But this will only match the entire input line and the last captured group.

What you can do is use the "g" switch.

So an example in Perl:

use strict;
use warnings;

my $str1 = "there are four lights";
my $str2 = "captain picard is the bomb";

foreach ( $str1, $str2 ) {
    my @a = ( $_ =~ /(\w+)\s?/g );
    print "captured groups are: " . join( "|", @a ) . "\n";



captured groups are: there|are|four|lights
captured groups are: captain|picard|is|the|bomb


So there is a solution if your language of choice supports the "g" equivalent (and I think most of them ...).

Hope this helps someone who was in the same position as me!




The problem is that the desired solution insists on using capture groups. C ++ provides a tool regex_token_iterator

for better management (C ++ example):

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main() {
    std::regex e (R"((?:^-Numbers)?\s*(\d+))");

    string input;

    while (getline(cin, input)) {
        std::regex_token_iterator<std::string::iterator> a{
            input.begin(), input.end(),
            e, 1,

        std::regex_token_iterator<std::string::iterator> end;
        while (a != end) {
            cout << *a << " - ";
        cout << '\n';

    return 0;



All Articles