Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
string ls_Subject
string ls_Pattern
oleobject loo_Sb
oleobject loo_Json
integer li_TimeoutMs
integer li_NumMatches
integer li_FirstNameIdx
integer li_LastNameIdx
oleobject loo_SbTemp
integer i

li_Success = 0

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

loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_Sb
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Sb.Append(ls_Subject)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.EmitCompact = 0

li_TimeoutMs = 2000
li_NumMatches = loo_Sb.RegexMatch(ls_Pattern,loo_Json,li_TimeoutMs)
if li_NumMatches < 0 then
    //  Probably an error in the regular expression.
    //  Suggestion: Use AI to help create and/or diagnose regular expressions.
    Write-Debug loo_Sb.LastErrorText
    destroy loo_Sb
    destroy loo_Json
    return
end if

//  Examine the matches:
Write-Debug loo_Json.Emit()

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

li_FirstNameIdx = loo_Json.IntOf("named.first")
li_LastNameIdx = loo_Json.IntOf("named.last")

loo_SbTemp = create oleobject
li_rc = loo_SbTemp.ConnectToNewObject("Chilkat.StringBuilder")

i = 0
li_NumMatches = loo_Json.SizeOfArray("match")
do while i < li_NumMatches

    loo_Json.I = i

    //  The replacement string for the first name will be all uppercase.
    loo_Json.J = li_FirstNameIdx
    loo_SbTemp.Clear()
    loo_Json.StringOfSb("match[i].group[j].cap",loo_SbTemp)
    loo_SbTemp.ToUppercase()
    loo_Json.UpdateSb("match[i].group[j].rep",loo_SbTemp)

    //  Append "on" to the last name.
    loo_Json.J = li_LastNameIdx
    loo_SbTemp.Clear()
    loo_Json.StringOfSb("match[i].group[j].cap",loo_SbTemp)
    loo_SbTemp.Append("on")
    loo_Json.UpdateSb("match[i].group[j].rep",loo_SbTemp)

    i = i + 1
loop

//  The JSON now has replacement strings:
Write-Debug loo_Json.Emit()

//  {
//    "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.
li_Success = loo_Sb.RegexReplace(loo_Json)
if li_Success = 0 then
    Write-Debug loo_Sb.LastErrorText
    destroy loo_Sb
    destroy loo_Json
    destroy loo_SbTemp
    return
end if

Write-Debug "Result after doing replacements:"
Write-Debug loo_Sb.GetAsString()

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


destroy loo_Sb
destroy loo_Json
destroy loo_SbTemp