DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sSubject
String sPattern
Handle hoSb
Variant vJson
Handle hoJson
Integer iTimeoutMs
Integer iNumMatches
Variant vSbTemp
Handle hoSbTemp
Integer i
String sTemp1
Move False To iSuccess
Move False To iSuccess
Move "John Anders, +_+_+ Mary Robins $$$$" To sSubject
Move "\w+\s+\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
// {
// "match": [
// {
// "group": [
// {
// "cap": "John Anders",
// "idx": 0,
// "len": 11
// }
// ]
// },
// {
// "group": [
// {
// "cap": "Mary Robins",
// "idx": 19,
// "len": 11
// }
// ]
// }
// ]
// }
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 full match is always in group 0.
Send ComClear To hoSbTemp
Get pvComObject of hoSbTemp to vSbTemp
Get ComStringOfSb Of hoJson "match[i].group[0].cap" vSbTemp To iSuccess
// Indicate that we wish to replace the full match with it's value converted to uppercase.
Get ComToUppercase Of hoSbTemp To iSuccess
Get pvComObject of hoSbTemp to vSbTemp
Get ComUpdateSb Of hoJson "match[i].group[0].rep" vSbTemp To iSuccess
Move (i + 1) To i
Loop
// The JSON now has replacement strings:
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// {
// "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.
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 ANDERS, +_+_+ MARY ROBINS $$$$
End_Procedure