CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] PCRE2 Regex Trouble - unable to extract subgroup matches of regex

  1. #1
    Join Date
    Sep 2004
    Posts
    117

    [RESOLVED] PCRE2 Regex Trouble - unable to extract subgroup matches of regex

    Hi Guys,

    I'm having some trouble extracting subgroup patterns from a regex match with PCRE2... sample code below....

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <string.h>
    #define PCRE2_STATIC
    #define PCRE2_CODE_UNIT_WIDTH 8
    #include "C:/pcre2-10.34/src/pcre2.h"
    
    using namespace std;
    
    int main(int argc, char** argv) {
    	
    	
      PCRE2_SPTR subject = (PCRE2_SPTR)"12345 1234124124";  // I should be able to extract two strings from this seperated by the space....
      PCRE2_SPTR pattern = (PCRE2_SPTR)"^([0-9]+)\\x20([0-9]+)$"; // I'm expecting to be able to extract two subgroups here....
    int errorcode;
    PCRE2_SIZE erroroffset;
    pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_DUPNAMES | PCRE2_UTF, &errorcode, &erroroffset, NULL);
    if (re) {
    	
    	
    
    	uint32_t groupcount = 0;
    	pcre2_pattern_info(re, PCRE2_INFO_BACKREFMAX, &groupcount);
    	pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
    	uint32_t options_exec = 0;
    	PCRE2_SIZE subjectlen = strlen((const char *) subject);
    	errorcode = pcre2_match(re, subject, subjectlen, 0, options_exec, match_data, NULL);
    	while (errorcode >= 0) {
    		
    		printf("%d\n",errorcode); //this prints 3
    		MessageBox(0,"The code gets here so something does match, but below we have NO capture groups... Why??","asdf",0);
    
    	  PCRE2_UCHAR *result;
          PCRE2_SIZE resultlen;
    		for (int i = 0; i < groupcount; i++) {
    			
    			
    			printf("%d\n",groupcount); // THE CODE DOES NOT GET HERE
    
    			//if (pcre2_substring_get_bynumber(match_data, i, &result, &resultlen)) {
    			    MessageBox(0,"How are there no capture groups if the above works?","asdf",0);
    
    				pcre2_substring_get_bynumber(match_data, i, &result, &resultlen);
            		printf("Matched:%.*s\n", (int) resultlen, (const char *) result);
    				pcre2_substring_free(result);
    		//	}
    		}
    		// Advance through subject, making sure not to get stuck at zero-length matches
    		PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
    		if (ovector[0] == ovector[1]) {
    			options_exec |= PCRE2_NOTEMPTY_ATSTART;
    		} else {
    			options_exec &= !PCRE2_NOTEMPTY_ATSTART;
    		}
    		errorcode = pcre2_match(re, subject, subjectlen, ovector[1], options_exec, match_data, NULL);
    	}
    	pcre2_match_data_free(match_data);
    	pcre2_code_free(re);
    } else {
    	// Syntax error in the regular expression at erroroffset
    	PCRE2_UCHAR error[256];
    	pcre2_get_error_message(errorcode, error, sizeof(error));
    	MessageBox(0,(char *)error,"asdf",0);
    }
    
    	
    	
    	
    	
    	
    	
    	
    	return 0;
    }
    Any idea what I'm doing wrong?



    Thx for the help...

    First post since like 15 years LOL!
    Best regards
    -c0ld

  2. #2
    Join Date
    Sep 2004
    Posts
    117

    Re: PCRE2 Regex Trouble - unable to extract subgroup matches of regex

    I found solution.... i put errorcount instead of groupcount below b/c that tells how many matches not groupcount.... regexbuddy software generates bad/incorrect C++ code

    Code:
    for (int i = 0; i <errorcount; i++) {

    Need to check retval of errorcode in whileloop which tells count of the matches

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)