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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:34:11+00:00 2026-05-23T15:34:11+00:00

can anyone please show me an example about using regex (regex.h) in C/C++ to

  • 0

can anyone please show me an example about using regex (regex.h) in C/C++ to search and/or extracting subpattern in a regex.

in javascript, it will be something like this,

var str = "the string contains 123 dots and 344 chars";
var r = /the string contains ([0-9].*?) dots and ([0-9].*?) chars/;
var res = r.exec(str);
var dots = res[1];
var chars = res[2];
alert('dots ' + dots + ' and chars ' + chars);

how can i do this using regex.h in c/c++ (not boost or any other libraries) ??

thanks,

  • 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-23T15:34:11+00:00Added an answer on May 23, 2026 at 3:34 pm

    There is no regex.h in standard C or standard C++, so I’m assuming you mean the POSIX regular expression library. C example:

    char const *str = "the string contains 123 dots and 344 chars";
    char const *re_str = "the string contains ([0-9].*?) dots and ([0-9].*?) chars";
    regex_t compiled;
    regmatch_t *groups;
    
    regcomp(&compiled, re_str, REG_EXTENDED);
    
    ngroups = compiled.re_nsub + 1;
    groups = malloc(ngroups * sizeof(regmatch_t));
    regexec(&compiled, str, ngroups, groups, 0);
    
    for (size_t i = 0; i < ngroups; i++) {
        if (groups[i].rm_so == (size_t)(-1))
            break;
        else {
            size_t len = groups[i].rm_eo - groups[i].rm_so;
            char buf[len + 1];
            memcpy(buf, str + groups[i].rm_so, len);
            buf[len] = '\0';
            puts(buf);
        }
    }
    free(groups);
    

    (Add your own error checking. For more details, see this answer.)

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

Sidebar

Related Questions

Can anyone please show me a specific example of a Symfony2 form entity update?
can anyone please suggest a good code example of vb.net/c# code to put the
Can anyone please recommend the best and easiest way to include multiple Javascript files
can anyone please let me know is it possible to show error provider icon
Can anyone please tell me if Certifying Authorities (CAs) are allowed to make modifications
Can anyone please provide a link to download the .NET 3.5 SDK? I checked
Can anyone please tell what is change request management tool and what is involved
Can anyone please tell me why compiling a C# application with a .cs file
Can anyone please suggest me a good sample code for zooming image in NSImageView.
Can anyone please tell me when should I use MessageContracts and when should I

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.