tags:

views:

46

answers:

1

Hello, I want to capture named substring with the pcre++ library.

I know the pcre library has the functionality for this, but pcre++ has not implemented this.

This is was I have now (just a simple example):

pcrepp::Pcre regex("test (?P<groupName>bla)");

if (regex.search("test bla"))
{
    // Get matched group by name
    int pos = pcre_get_stringnumber(
        regex.get_pcre(),
        "groupName"
    );
    if (pos == PCRE_ERROR_NOSUBSTRING) return;

    // Get match
    std::string temp = regex[pos - 1];

    std::cout << "temp: " << temp << "\n";
}

If I debug, pos return 1, and that is right, (?Pbla) is the 1th submatch (0 is the whole match). It should be ok. But... regex.matches() return 0. Why is that :S ?

Btw. I do regex[pos - 1] because pcre++ reindexes the result with 0 pointing to the first submatch, so 1. So 1 becomes 0, 2 becomes 1, 3 becomes 2, etc.

Does anybody know how to fix this?

A: 

My mistake unfortunately, I tested the regex in my real program and there the regex was different. I used something like this:

(?:/(?P<controller>[^/]+)(?:/(?P<action>[^/]+))?)?

So the group name to number conversion goes well, but when i try to access the group i get index of range because of the (?: ... )? groups. I just added a check if the group index i in the correct range, it is i could use the group.

Sorry for asking it here too early.

VDVLeon