Question: **for those asking, the {0} allows selection of any one block within the sResult string separated by the | 0 is the first block** 

it needs to be dynamic for future expansion as that number will be configurable by users 

So I am working on a regex to extract 1 portion of a string, however while it matches the results return are not what is expected.

```
    std::string sResult = "MATCH_ME|BUT|NOT|ANYTHNG|ELSE";
    std::regex pattern("^(?:[^|]+[|]){0}([^|;]+)");
    std::smatch regMatch;

    std::regex_search(sResult, regMatch, pattern);
    if(regMatch[1].matched)
    {
      for( int i = 0; i < regMatch.size(); i++)
      {
           //SUBMATCH 0 = "MATCH_ME|BUT|NOT|ANYTHNG|ELSE"
           //SUBMATCH 1 = "BUT|NOT|ANYTHNG|ELSE"
        std::ssub_match sm = regMatch[i];
        bValid = strcmp(regMatch[i].str().c_str(), pzPoint->_ptrTarget->_pzTag->szOPCItem);
      }
    }

```

For some reason I cannot figure out the code to get me just the MATCH\_ME back so I can compare it to expected results list on the C++ side.

Anyone have any ideas on where I went wrong here.

Answer: 
The following code example shows how to do what you are after - you compile this, then call it with a single numerical argument to extract that element of the input:

```
#include <iostream>
#include <cstring>
#include <regex>

int main(int argc, char *argv[]) {

    char pat[100];
    if (argc > 1) {
      sprintf(pat, "^(?:[^|]+[|]){%s}([^|;]+)", argv[1]);
      std::string sResult = "MATCH_ME|BUT|NOT|ANYTHNG|ELSE";
      std::regex pattern(pat);
      std::smatch regMatch;

      std::regex_search(sResult, regMatch, pattern);
      if(regMatch[1].matched)
      {
        std::ssub_match sm = regMatch[1];
        std::cout << "The match is " << sm << std::endl;
//bValid = strcmp(regMatch[i].str().c_str(), pzPoint->_ptrTarget->_pzTag->szOPCItem);
      }
    }
    return 0;
}

```

Creating an executable called `match`, you can then do

```
>> match 2
The match is NOT

```

which is what you wanted.

The regex, it turns out, works just fine - although as a matter of preference I would use `\|` instead of `[|]` for the first part.
Turns out the problem was on the C side in extracting the match, it had to be done more directly, below is the code that gets me exactly what I wanted out of the string so I can use it later. 

```
std::string sResult = "MATCH_ME|BUT|NOT|ANYTHNG|ELSE";
std::regex pattern("^(?:[^|]+[|]){0}([^|;]+)");
std::smatch regMatch;

std::regex_search(sResult, regMatch, pattern);
if(regMatch[1].matched)
{
   std::string theMatchedPortion = regMatch[1];
   //the issue was not with the regex but in how I was retrieving the results.
   //theMatchedPortion now equals "MATCH_ME" and by changing the number associated 
        with it I can navigate through the string
}

```