Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) 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

Note: This example requires Chilkat v11.1.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sSubject
    String sPattern
    Handle hoSb
    Variant vJson
    Handle hoJson
    Integer iTimeoutMs
    Integer iNumMatches
    Integer iFirstNameIdx
    Integer iLastNameIdx
    Variant vSbTemp
    Handle hoSbTemp
    Integer i
    String sTemp1

    Move False To iSuccess

    Move "John Anders, +_+_+ Mary Robins $$$$" To sSubject
    Move "(?<first>\w+)\s+(?<last>\w+)" To sPattern

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get ComAppend Of hoSb sSubject To iSuccess

    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

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

    Get ComIntOf Of hoJson "named.first" To iFirstNameIdx
    Get ComIntOf Of hoJson "named.last" To iLastNameIdx

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTemp
    If (Not(IsComObjectCreated(hoSbTemp))) Begin
        Send CreateComObject of hoSbTemp
    End

    Move 0 To i
    Get ComSizeOfArray Of hoJson "match" To iNumMatches
    While (i < iNumMatches)

        Set ComI Of hoJson To i

        // The replacement string for the first name will be all uppercase.
        Set ComJ Of hoJson To iFirstNameIdx
        Send ComClear To hoSbTemp
        Get pvComObject of hoSbTemp to vSbTemp
        Get ComStringOfSb Of hoJson "match[i].group[j].cap" vSbTemp To iSuccess
        Get ComToUppercase Of hoSbTemp To iSuccess
        Get pvComObject of hoSbTemp to vSbTemp
        Get ComUpdateSb Of hoJson "match[i].group[j].rep" vSbTemp To iSuccess

        // Append "on" to the last name.
        Set ComJ Of hoJson To iLastNameIdx
        Send ComClear To hoSbTemp
        Get pvComObject of hoSbTemp to vSbTemp
        Get ComStringOfSb Of hoJson "match[i].group[j].cap" vSbTemp To iSuccess
        Get ComAppend Of hoSbTemp "on" To iSuccess
        Get pvComObject of hoSbTemp to vSbTemp
        Get ComUpdateSb Of hoJson "match[i].group[j].rep" vSbTemp To iSuccess

        Move (i + 1) To i
    Loop

    // The JSON now has replacement strings:
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // {
    //   "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.
    Get pvComObject of hoJson to vJson
    Get ComRegexReplace Of hoSb vJson To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSb To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Result after doing replacements:"
    Get ComGetAsString Of hoSb To sTemp1
    Showln sTemp1

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


End_Procedure

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.