Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.1.0+

Regular Expression Replace Capture Groups

See more Regular Expressions Examples

Demonstrates replacing capture groups for a regular expression.

Note: Chilkat uses PCRE2. See PCRE2 Regular Expressions
Also see: PCRE2 Performance

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    subject.s = "John Anders, +_+_+ Mary Robins $$$$"
    pattern.s = "(?<first>\w+)\s+(?<last>\w+)"

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

    CkStringBuilder::ckAppend(sb,subject)

    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)

    ;  There are 2 matches:

    ;  {
    ;    "named": {
    ;      "first": 1,
    ;      "last": 2
    ;    },
    ;    "match": [
    ;      {
    ;        "group": [
    ;          {
    ;            "cap": "John Anders",
    ;            "idx": 0,
    ;            "len": 11
    ;          },
    ;          {
    ;            "cap": "John",
    ;            "idx": 0,
    ;            "len": 4
    ;          },
    ;          {
    ;            "cap": "Anders",
    ;            "idx": 5,
    ;            "len": 6
    ;          }
    ;        ]
    ;      },
    ;      {
    ;        "group": [
    ;          {
    ;            "cap": "Mary Robins",
    ;            "idx": 19,
    ;            "len": 11
    ;          },
    ;          {
    ;            "cap": "Mary",
    ;            "idx": 19,
    ;            "len": 4
    ;          },
    ;          {
    ;            "cap": "Robins",
    ;            "idx": 24,
    ;            "len": 6
    ;          }
    ;        ]
    ;      }
    ;    ]
    ;  }

    ;  To replace capture groups, write code to examine each capture group within
    ;  each match, and provide a replacement string.
    ;  Then call RegexReplace.

    ;  For example, let's capitalize the first names, and add append "on" to the last name.
    ;  After doing replacements, we should get:  JOHN Anderson, +_+_+ MARY Robinson $$$$

    firstNameIdx.i = CkJsonObject::ckIntOf(json,"named.first")
    lastNameIdx.i = CkJsonObject::ckIntOf(json,"named.last")

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

    i.i = 0
    numMatches = CkJsonObject::ckSizeOfArray(json,"match")
    While i < numMatches

        CkJsonObject::setCkI(json, i)

        ;  The replacement string for the first name will be all uppercase.
        CkJsonObject::setCkJ(json, firstNameIdx)
        CkStringBuilder::ckClear(sbTemp)
        CkJsonObject::ckStringOfSb(json,"match[i].group[j].cap",sbTemp)
        CkStringBuilder::ckToUppercase(sbTemp)
        CkJsonObject::ckUpdateSb(json,"match[i].group[j].rep",sbTemp)

        ;  Append "on" to the last name.
        CkJsonObject::setCkJ(json, lastNameIdx)
        CkStringBuilder::ckClear(sbTemp)
        CkJsonObject::ckStringOfSb(json,"match[i].group[j].cap",sbTemp)
        CkStringBuilder::ckAppend(sbTemp,"on")
        CkJsonObject::ckUpdateSb(json,"match[i].group[j].rep",sbTemp)

        i = i + 1
    Wend

    ;  The JSON now has replacement strings:
    Debug CkJsonObject::ckEmit(json)

    ;  {
    ;    "named": {
    ;      "first": 1,
    ;      "last": 2
    ;    },
    ;    "match": [
    ;      {
    ;        "group": [
    ;          {
    ;            "cap": "John Anders",
    ;            "idx": 0,
    ;            "len": 11
    ;          },
    ;          {
    ;            "cap": "John",
    ;            "idx": 0,
    ;            "len": 4,
    ;            "rep": "JOHN"
    ;          },
    ;          {
    ;            "cap": "Anders",
    ;            "idx": 5,
    ;            "len": 6,
    ;            "rep": "Anderson"
    ;          }
    ;        ]
    ;      },
    ;      {
    ;        "group": [
    ;          {
    ;            "cap": "Mary Robins",
    ;            "idx": 19,
    ;            "len": 11
    ;          },
    ;          {
    ;            "cap": "Mary",
    ;            "idx": 19,
    ;            "len": 4,
    ;            "rep": "MARY"
    ;          },
    ;          {
    ;            "cap": "Robins",
    ;            "idx": 24,
    ;            "len": 6,
    ;            "rep": "Robinson"
    ;          }
    ;        ]
    ;      }
    ;    ]
    ;  }

    ;  Call RegexReplace to update the StringBuilder with the replacements.
    success = CkStringBuilder::ckRegexReplace(sb,json)
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sb)
        CkStringBuilder::ckDispose(sb)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbTemp)
        ProcedureReturn
    EndIf

    Debug "Result after doing replacements:"
    Debug CkStringBuilder::ckGetAsString(sb)

    ;  Result after doing replacements:
    ;  JOHN Anderson, +_+_+ MARY Robinson $$$$


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


    ProcedureReturn
EndProcedure