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

  • SEARCH
  • Home
  • 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 143727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:08:28+00:00 2026-05-11T08:08:28+00:00

I am trying to find out how many regex matches are in a string.

  • 0

I am trying to find out how many regex matches are in a string. I’m using an iterator to iterate the matches, and and integer to record how many there were.

long int before = GetTickCount(); string text;  boost::regex re('^(\\d{5})\\s(\\d{8})\\s(.*)\\s(.*)\\s(.*)\\s(\\d{8})\\s(.{1})$'); char * buffer; long length; long count; ifstream f;   f.open('c:\\temp\\test.txt', ios::in | ios::ate); length = f.tellg(); f.seekg(0, ios::beg);  buffer = new char[length];  f.read(buffer, length); f.close();  text = buffer; boost::sregex_token_iterator itr(text.begin(), text.end(), re, 0); boost::sregex_token_iterator end;  count = 0; for(; itr != end; ++itr) {     count++; }  long int after = GetTickCount(); cout << 'Found ' << count << ' matches in ' << (after-before) << ' ms.' << endl; 

In my example, count always returns 1, even if I put code in the for loop to show the matches (and there are plenty). Why is that? What am I doing wrong?

Edit

TEST INPUT:

12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 

OUTPUT (without matches):

Found 1 matches in 16 ms.

If I change the for loop to this:

count = 0; for(; itr != end; ++itr) {     string match(itr->first, itr->second);     cout << match << endl;     count++; } 

I get this as output:

12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N 12345 12345678 SOME NAME SOMETHING 88888888 N Found 1 matches in 47 ms. 
  • 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. 2026-05-11T08:08:28+00:00Added an answer on May 11, 2026 at 8:08 am

    Heh. Your problem is your regex. Change your (.\*)s to (.\*?)s (assuming that’s supported). You think you’re seeing each line being matched, but in fact you’re seeing the entire text being matched because your pattern is greedy.

    To see the issue illustrated, change the debug output in your loop to:

    cout << '[' << match << ']' << endl; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out if there is any way to elevate a specific
I'm trying to find out whether I should be using business critical logic in
I am trying to find out how many times a gem was installed via
I was trying out an experiment with Python, trying to find out how many
I'm trying to find out if there is a jQuery plugin that can has
been trying to find out if there is a way to pre-write the tag
In a C++ multi-threaded application with many classes, i am trying to find out
I want to find out how many lines a string will have when set
Im trying to find out a good JavaScript library that can create a nice
Hi was trying to find out time taken for the execution of an API

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.