GSAK (Geocaching Swiss Army Knife)
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 Backreference 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.
Back refereence 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 refererences (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 preceeding 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
2. - Now see how poorly this works if we forget to create a back reference (the brackets are missing)
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"
4. Now see how poorly this code works if we forget to add the "$" in the 4th parameter:
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:
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:
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-2011 CWE Computer Services |