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

Regular Expression Replace Full Matches

See more Regular Expressions Examples

Demonstrates replacing the full matches of 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
oleobject loo_SbTemp
integer i

li_Success = 0

ls_Subject = "John Anders, +_+_+ Mary Robins $$$$"
ls_Pattern = "\w+\s+\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()

//  {
//    "match": [
//      {
//        "group": [
//          {
//            "cap": "John Anders",
//            "idx": 0,
//            "len": 11
//          }
//        ]
//      },
//      {
//        "group": [
//          {
//            "cap": "Mary Robins",
//            "idx": 19,
//            "len": 11
//          }
//        ]
//      }
//    ]
//  }

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 full match is always in group 0.
    loo_SbTemp.Clear()
    loo_Json.StringOfSb("match[i].group[0].cap",loo_SbTemp)

    //  Indicate that we wish to replace the full match with it's value converted to uppercase.
    loo_SbTemp.ToUppercase()
    loo_Json.UpdateSb("match[i].group[0].rep",loo_SbTemp)

    i = i + 1
loop

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

//  {
//    "match": [
//      {
//        "group": [
//          {
//            "cap": "John Anders",
//            "idx": 0,
//            "len": 11,
//            "rep": "JOHN ANDERS"
//          }
//        ]
//      },
//      {
//        "group": [
//          {
//            "cap": "Mary Robins",
//            "idx": 19,
//            "len": 11,
//            "rep": "MARY ROBINS"
//          }
//        ]
//      }
//    ]
//  }

//  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 ANDERS, +_+_+ MARY ROBINS $$$$


destroy loo_Sb
destroy loo_Json
destroy loo_SbTemp