Sample code for 30+ languages & platforms
C++

Regular Expression with Multiple Matches and Named Capture Groups

See more Regular Expressions Examples

Demonstrates regular expressions with named capture groups and multiple matches.

Chilkat C++ Downloads

C++
#include <CkStringBuilder.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkStringBuilder sb;
    bool crlf = true;
    sb.AppendLine("Name: John Smith",crlf);
    sb.AppendLine("Name: Jack Johnson",crlf);
    sb.AppendLine("Name: Mary Adams",crlf);

    std::cout << sb.getAsString() << "\r\n";

    //  We have the following string:
    //  Name: John Smith
    //  Name: Jack Johnson
    //  Name: Mary Adams

    const char *pattern = "Name:\\s+(?<first>\\w+)\\s+(?<last>\\w+)";
    CkJsonObject json;
    json.put_EmitCompact(false);

    int timeoutMs = 2000;
    int numMatches = sb.RegexMatch(pattern,json,timeoutMs);
    if (numMatches < 0) {
        //  Probably an error in the regular expression.
        //  Suggestion: Use AI to help create and/or diagnose regular expressions.
        std::cout << sb.lastErrorText() << "\r\n";
        return;
    }

    //  Examine the matches:
    std::cout << json.emit() << "\r\n";

    //  Here is the JSON showing the matches.
    //  Important:  Capture group 0 always contains the entire match — that is, the portion of the input string that matches the full regular expression.

    //  {
    //    "named": {
    //      "first": 1,
    //      "last": 2
    //    },
    //    "match": [
    //      {
    //        "group": [
    //          {
    //            "cap": "Name: John Smith",
    //            "idx": 0,
    //            "len": 16
    //          },
    //          {
    //            "cap": "John",
    //            "idx": 6,
    //            "len": 4
    //          },
    //          {
    //            "cap": "Smith",
    //            "idx": 11,
    //            "len": 5
    //          }
    //        ]
    //      },
    //      {
    //        "group": [
    //          {
    //            "cap": "Name: Jack Johnson",
    //            "idx": 18,
    //            "len": 18
    //          },
    //          {
    //            "cap": "Jack",
    //            "idx": 24,
    //            "len": 4
    //          },
    //          {
    //            "cap": "Johnson",
    //            "idx": 29,
    //            "len": 7
    //          }
    //        ]
    //      },
    //      {
    //        "group": [
    //          {
    //            "cap": "Name: Mary Adams",
    //            "idx": 38,
    //            "len": 16
    //          },
    //          {
    //            "cap": "Mary",
    //            "idx": 44,
    //            "len": 4
    //          },
    //          {
    //            "cap": "Adams",
    //            "idx": 49,
    //            "len": 5
    //          }
    //        ]
    //      }
    //    ]
    //  }

    //  The capture group index is obtained by looking up the name in the JSON result.
    //  For example:

    int idx_first = json.IntOf("named.first");
    int idx_last = json.IntOf("named.last");

    int i = 0;
    int matchCount = json.SizeOfArray("match");
    while (i < matchCount) {
        std::cout << "Match " << (i + 1) << ":" << "\r\n";
        json.put_I(i);

        json.put_J(idx_first);
        std::cout << "first: " << json.stringOf("match[i].group[j].cap") << "\r\n";

        json.put_J(idx_last);
        std::cout << "first: " << json.stringOf("match[i].group[j].cap") << "\r\n";

        std::cout << "" << "\r\n";
        i = i + 1;
    }

    //  Output is: 

    //  Match 1:
    //  first: John
    //  first: Smith
    //  
    //  Match 2:
    //  first: Jack
    //  first: Johnson
    //  
    //  Match 3:
    //  first: Mary
    //  first: Adams
    }