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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:13:51+00:00 2026-05-14T14:13:51+00:00

i have this C++ program (actually it’s just a snippet) : #include <iostream> #include

  • 0

i have this C++ program (actually it’s just a snippet) :

#include <iostream>
#include <pcre.h>
#include <string>

using namespace std;

int main(){    
    string pattern = "<a\\s+href\\s*=\\s*\"([^\"]+)\"",
           html = "<html>\n"
                  "<body>\n"
                  "<a href=\"example_link_1\"/>\n"
                  "<a href=\"example_link_2\"/>\n"
                  "<a href=\"example_link_3\"/>\n"
                  "</body>\n"
                  "</html>";
    int            i, ccount, rc,
                *offsets,
                 eoffset;
    const char  *error;
    pcre         *compiled;

    compiled = pcre_compile( pattern.c_str(), PCRE_CASELESS | PCRE_MULTILINE, &error, &eoffset, 0 );
    if( !compiled ){
        cerr << "Error compiling the regexp!!" << endl;
        return 0;
    }

    rc = pcre_fullinfo( compiled, 0, PCRE_INFO_CAPTURECOUNT, &ccount );

    offsets = new int[ 3 * (ccount + 1) ];

    rc = pcre_exec( compiled, 0, html.c_str(), html.length(), 0, 0, offsets, 3 * (ccount + 1) );

    if( rc >= 0 ){
        for( i = 1; i < rc; ++i ){
            cout << "Match : " << html.substr( offsets[2*i], offsets[2*i+1] - offsets[2*i] ) << endl;
        }
    }
    else{
        cout << "Sorry, no matches!" << endl;
    }

    delete [] offsets;

    return 0;
}

As you can see, i’m trying to match html links inside a buffer with the given regular expression (the \\s is \s escaped for C/C++ strings).
But, even if in the buffer there are 3 links and the regexp is compiled with the PCRE_CASELESS and PCRE_MULTILINE flags, i match only one element :

Match : example_link_1

Note: I start the loop fro index 1 because the pcre library returns the string that matched (not the match itself) as the first element, and the matches follows.

What’s wrong with this code? The regexp itself i think it’s correct (tried in PHP for instance).

  • 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-14T14:13:51+00:00Added an answer on May 14, 2026 at 2:13 pm

    Well, it’s not supposed to return all matches. Just think of it, you ask for capturecount, which is something like one or two (that is, either the whole match and one subexpression, or just subexpression, I don’t remember, I’d guess two). And how would you expect it to know how many matches are in the string you’ve never passed to it? And you don’t expect the thing to return three matches in the array, do you? And if you’ve had three thousands?

    It’s been a while since I dealt with pcre api, but I would think you need to loop and match against the rest of the string again.

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

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something like this: update T set RowNumInFlow = @TheNumber FROM… May 14, 2026 at 10:59 pm
  • Editorial Team
    Editorial Team added an answer You need to look at the individual framework change logs:… May 14, 2026 at 10:59 pm
  • Editorial Team
    Editorial Team added an answer See the documentation on the full-text search support in mysql:… May 14, 2026 at 10:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.