GSAK (Geocaching Swiss Army Knife)
Contents - Index

RegExReplace (function)

RegExReplace(sExpression, sData, sReplaceWith,[sBackRefSymbol]) : string

sExpression - The regular expression to match
sData - The string to search for this regular expression
sReplaceWith - The string that will replace any matched expression 
sBackRefSymbol - Optional Back reference symbol (see notes)

This enables you to replace the matching regex data with a string. The problem with the existing Replace() function is that you need to know in advance exactly what it is that you are replacing. With the power of regular expressions your matching data can vary significantly and many times. This function does not care what your matching data is, it just faithfully replaces the matched data with your replacement string.
 

$data = "My pin numbers are 1234 and 5678"
$data = RegExReplace("\d*",$data,"*removed*")
cancel msg=$data




Back reference replacements are supported by including a back reference symbol. This 4th parameter is a string value, and will default to an empty string if you leave it out. To use back reference replacements, you must enter the character "$" or "\" in this parameter.

The GSAK regex engine supports this feature by what is known as numbered back references (these are virtually the same as sub expressions as used in the RegExSub() function). The back references are referenced by either "$" or "\" then a number (the number of your back reference/sub expression)

You can escape unwanted back references in your replacement string by preceding your trigger character with the same character

The best way to get a grip on this is to see a few examples:

1 - Replace all of our matched string by using the whole back reference
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("(GC.*? )",$test,"http://coord.info/$1" + $_CrLf,"$")
msgok msg=$test

 

 
2. - Now see how poorly this works if we forget to create a back reference (the brackets are missing)
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("GC.*? ",$test,"http://coord.info/$1" + $_CrLf,"$")
msgok msg=$test

 

 
3. Now we want to convert all GCXXXX codes to OXXXXX codes. This time we still want to *match* the full code, but we only want to replace the "GC" part. We need to break this into two back references so we can just access the part after "GC"
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("(GC)(.*? )",$test,"OX$2" + $_Crlf,"$")
msgok msg=$test

 

  
4. Now see how poorly this code works if we forget to add the "$" in the 4th parameter:
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("(GC)(.*? )",$test,"OX$2" + $_Crlf)
msgok msg=$test

 

 
5. If for some reason your replacement string is likely to have many "$" characters in it, then you may be better off using the "\" as your trigger character:
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("(GC)(.*? )",$test,"OX\2" + $_Crlf,"\")
msgok msg=$test

 

 
6. Finally, what if we *really* wanted to make part of the replacement string include the literal string "$2" - we need to escape the first instance (or the ones we don't want replaced) so it does not trigger:
 

$test = "GC1234 GC12345 GC12346 "
$test = RegExReplace("(GC)(.*? )",$test,"$$2BB$2" + $_Crlf,"$")
msgok msg=$test

 

 
Note1: The trigger characters "\" and"$" are fixed by the RegEx engine and can't be changed.

Note2: You may want to consider using the "\" as your trigger. The problem with using "$" is that your macro might have defined some variables called $1, $2, $3 etc and variable substitution will mess things up. Alternatively, if want to make sure you protect yourself when using the "$" character as the trigger, you may want to turn off variable substitution while executing your RegExReplace() code 

Alpha List         Category List
Copyright 2004-2019 CWE Computer Services  
Privacy Policy Contact