Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5969957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:18:19+00:00 2026-05-22T20:18:19+00:00

In Perl, I can do this: $text = ‘1747239’; @matches = ($text =~ m/(\d)/g);

  • 0

In Perl, I can do this:

$text = '1747239';
@matches = ($text =~ m/(\d)/g);
# @matches now contains ('1', '7', '4', '7', '2', '3', '9')

Using C++ regex matching, what’s the best way to replicate this behaviour so
that I get a match set including all the matches?

I have this at the moment:-

compiledRegex = std::regex(regex, std::tr1::regex_constants::extended);
regex_search(text, results, compiledRegex);

int count = results.size();
// Alloc pointer array based on count * sizeof(mystruct).
for ( std::cmatch::iterator match = results.begin(); 
      match != results.end(); 
      ++match )
{
    // Do something with match;
}

However this will only give me the first match, just like Perl without the /g which is fine, but I’d like the /g effect.

So, is there a good way to do it, or must I keep running the regex over and over?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:18:20+00:00Added an answer on May 22, 2026 at 8:18 pm

    You should call regex_search several times. Its return value specifies whether there are more matches. Each time you call it you get a new match. The iterator returned by results goes over the group submatches defined in your regular expression. The first entry is always the entire match, that’s why in your case count == 1

    std::string::const_iterator text_iter = text.cbegin();
    compiledRegex = std::regex(regex, std::tr1::regex_constants::extended);
    
    while (regex_search(text_iter, text.end(), results, compiledRegex))
    {
        int count = results.size();
        // Alloc pointer array based on count * sizeof(mystruct).
        for ( std::cmatch::iterator group = results.begin();
              group != results.end();
              ++group )
        {
            // If you uses grouping in your search here you can access each group
        }
    
       std::cout << std::string(results[0].first, results[0].second) << endl;
       text_iter = results[0].second;
    }
    

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Tcl. In Perl I can do this: $ perl -e 'for ($i
I know this is possible in Perl, but I was wondering if this can
How can I do this Perl code in PHP? print unpack (H*, pack (B*,
Can someone explain this to me. I am pretty good with perl regular expressions
The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use
I tried this example in Perl. Can someone explain why is it true? if
How can I run my perl CGI script without apache? This is for testing
I've seen this one-liner perl -lane '$_{$F[0]}+=$F[1]}print$_ $_{$_}for keys%_;{' file here: How can I
I am new to perl, and can't seem to find answers about this anywhere.
I created csv file with this perl module Text::CSV_XS on windows: Below a snippet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.