×
Menu
Index

3.6.2.16. Regex Match/Replace

It is possible to use the Regular Expression object to match and replace using regular expressions directly from ChronoScan.
We have prepared a simple sample Job that utilizes the regex functionality to match an expression, get it's result and replace it with the desired value.
This sample Job has three custom buttons with the following functions:
 
 
The first step is to enter the desired regular expression on the Regex Pattern field. The String to match field should contain the desired value to be tested against the regex.
 
Test MATCH - This button tests the regular expression against the value on the String to match field. It returns TRUE or FALSE on the Is a match found field:
This is the function:
UserField_Is_a_match_found.value=""
UserField_Result_of_match.value=""
 
regxPat=UserField_Regex_Pattern.value
 
Dim oRE, bMatch
Set oRE = New RegExp
oRE.Pattern = regxPat ' You can put the regex directly here
UserField_Is_a_match_found.value= oRE.Test(UserField_String_to_match.value)
 
Get Result of Match - This retrieves all results for the regular expression and put the results inside the Result of match field:
This is the function:
UserField_Is_a_match_found.value=""
UserField_Result_of_match.value=""
 
Dim oRE, oMatches
Set oRE = New RegExp
oRE.Pattern = UserField_Regex_Pattern.value
oRE.Global = True
Set oMatches = oRE.Execute(UserField_String_to_match.value)
 
For Each oMatch In oMatches
    UserField_Result_of_match.value ="Match: " & oMatch.Value & " At: " & CStr(oMatch.FirstIndex + 1)
Next
 
Replace - The Replace button will replace the matches found with the value on the Enter string to replace field:
This is the function:
UserField_Is_a_match_found.value=""
UserField_Result_of_match.value=""
UserField_Replacement_result.value=""
 
Set oRE = New RegExp
oRE.Global = True
oRE.Pattern =UserField_Regex_Pattern.value
UserField_Replacement_result.value=oRE.Replace(UserField_String_to_match.value, UserField_Enter_string_to_replace.value)