Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    crlf.i = 1
    CkStringBuilder::ckAppendLine(sb,"Name: John Smith",crlf)
    CkStringBuilder::ckAppendLine(sb,"Name: Jack Johnson",crlf)
    CkStringBuilder::ckAppendLine(sb,"Name: Mary Adams",crlf)

    Debug CkStringBuilder::ckGetAsString(sb)

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

    pattern.s = "Name:\s+(?<first>\w+)\s+(?<last>\w+)"
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)

    timeoutMs.i = 2000
    numMatches.i = CkStringBuilder::ckRegexMatch(sb,pattern,json,timeoutMs)
    If numMatches < 0
        ; Probably an error in the regular expression.
        ; Suggestion: Use AI to help create and/or diagnose regular expressions.
        Debug CkStringBuilder::ckLastErrorText(sb)
        CkStringBuilder::ckDispose(sb)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; Examine the matches:
    Debug CkJsonObject::ckEmit(json)

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

    idx_first.i = CkJsonObject::ckIntOf(json,"named.first")
    idx_last.i = CkJsonObject::ckIntOf(json,"named.last")

    i.i = 0
    matchCount.i = CkJsonObject::ckSizeOfArray(json,"match")
    While i < matchCount
        Debug "Match " + Str(i + 1) + ":"
        CkJsonObject::setCkI(json, i)

        CkJsonObject::setCkJ(json, idx_first)
        Debug "first: " + CkJsonObject::ckStringOf(json,"match[i].group[j].cap")

        CkJsonObject::setCkJ(json, idx_last)
        Debug "first: " + CkJsonObject::ckStringOf(json,"match[i].group[j].cap")

        Debug ""
        i = i + 1
    Wend

    ; Output is: 

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


    CkStringBuilder::ckDispose(sb)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure