Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSb
    Boolean iCrlf
    String sPattern
    Variant vJson
    Handle hoJson
    Integer iTimeoutMs
    Integer iNumMatches
    Integer iIdx_first
    Integer iIdx_last
    Integer i
    Integer iMatchCount
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Move True To iCrlf
    Get ComAppendLine Of hoSb "Name: John Smith" iCrlf To iSuccess
    Get ComAppendLine Of hoSb "Name: Jack Johnson" iCrlf To iSuccess
    Get ComAppendLine Of hoSb "Name: Mary Adams" iCrlf To iSuccess

    Get ComGetAsString Of hoSb To sTemp1
    Showln sTemp1

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

    Move "Name:\s+(?<first>\w+)\s+(?<last>\w+)" To sPattern
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Set ComEmitCompact Of hoJson To False

    Move 2000 To iTimeoutMs
    Get pvComObject of hoJson to vJson
    Get ComRegexMatch Of hoSb sPattern vJson iTimeoutMs To iNumMatches
    If (iNumMatches < 0) Begin
        // Probably an error in the regular expression.
        // Suggestion: Use AI to help create and/or diagnose regular expressions.
        Get ComLastErrorText Of hoSb To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the matches:
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // 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:

    Get ComIntOf Of hoJson "named.first" To iIdx_first
    Get ComIntOf Of hoJson "named.last" To iIdx_last

    Move 0 To i
    Get ComSizeOfArray Of hoJson "match" To iMatchCount
    While (i < iMatchCount)
        Showln "Match " (i + 1) ":"
        Set ComI Of hoJson To i

        Set ComJ Of hoJson To iIdx_first
        Get ComStringOf Of hoJson "match[i].group[j].cap" To sTemp1
        Showln "first: " sTemp1

        Set ComJ Of hoJson To iIdx_last
        Get ComStringOf Of hoJson "match[i].group[j].cap" To sTemp1
        Showln "first: " sTemp1

        Showln ""
        Move (i + 1) To i
    Loop

    // Output is: 

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


End_Procedure