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 8203359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:31:33+00:00 2026-06-07T07:31:33+00:00

I am using Dev-c++ IDE to compile my C (WIN32 API) programs. I am

  • 0

I am using Dev-c++ IDE to compile my C (WIN32 API) programs.

I am using regex lirary provided by http://gnuwin32.sourceforge.net/packages/regex.htm

I am using this documentation for reference and the same has been provided from the above site… http://pubs.opengroup.org/onlinepubs/009695399/functions/regcomp.html

Following is the Code:

#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <regex.h>
#include <conio.h>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])
{
    int a;
    regex_t re;
    char str[128] = "onces sam lived with samle to win samile hehe sam hoho sam\0";
    regmatch_t pm;

    a = regcomp(&re,"sam", 0);
    if(a!=0)
    {
        puts("Invalid Regex");
        getch();
        return 0;
    }
    
    a = regexec(&re, &str[0], 1, &pm, REG_EXTENDED);    
    printf("\n first match at %d",pm.rm_eo);        
    
    int cnt = 0;
    
    while(a==0)
    {
        a = regexec(&re, &str[0] + pm.rm_eo, 1, &pm, 0);

        printf("\n next match %d",pm.rm_eo);        
        
        cnt++;        
        if(cnt>6)break;
    }
    
    getch();  
    return EXIT_SUCCESS;
}

The while loop goes infinite displaying the first and second end position of the matching string and not going further.

I have used the cnt variable to check for 6 turns and then i am breaking the loop to stop the infinite run.

The Output is:

first match at 9

next match 15

next match 9

next match 15

next match 9

next match 15

What am i missing here?

  • 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-06-07T07:31:36+00:00Added an answer on June 7, 2026 at 7:31 am

    Try this instead:

    int cnt = 0;
    int offset = 0;
    a = regexec(&re, &str[0], 1, &pm, REG_EXTENDED);
    while(a==0) {
        printf("\n %s match at %d", offset ? "next" : "first", offset+pm.rm_so);
        offset += pm.rm_eo;
        cnt++;
        a = regexec(&re, &str[0] + offset, 1, &pm, 0);
    }
    

    You were not actually stepping through your string, which was what caused the unending loop.

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

Sidebar

Related Questions

I am a newbie java dev using netbeans IDE 7.1.1 and im watching this
I'm using OpenGL with C++ in dev-c++ IDE. When I draw a circle, nothing
I am using http://dev.sencha.com/deploy/ext-4.0.7-gpl/examples/portal/portal.html to build a portal. Obviously, i have custom windows inside
I'm using Dev-C++ IDE for C programming. I want to put my project in
Using VS2008 on my local dev machine, when I run my asp.net Web Application
I am using Dev-C++ IDE, and now I am trying to do the file
I am using Eclipse IDE with few plug-ins for PHP dev. I am using
I am trying to develop a project in C (on Dev-Cpp IDE) using SQLite
I'm using Dev C++ to write a simple C program and I need to
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.

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.