ph_replaceregex PowerHome formula function
Description
Performs a regular expression search and replace on a string of data.
Syntax
ph_replaceregex ( pattern, replace, data, start, flags, localstart, locallength, localreplength )
Argument |
Description |
pattern
|
String. A regular expression search pattern.
|
replace
|
String. A regular expression replacement pattern.
|
data
|
String. The string in which to perform the search and replace.
|
start
|
Long. The position within the data in which to start the search. Use 1 to start at the beginning.
|
flags
|
Integer. Flags that control how the search is performed. Add individual flag values together. Add 1 to cause the search to match case. Add 2 to cause the search to ignore cr/lf's within the data.
|
localstart
|
Integer. The index of a local variable in which to have the start of the found data returned. Use 0 to not have the start returned.
|
locallength
|
Ingeger. The index of a local variable in which to have the length of the found data returned. Use 0 to not have the length returned.
|
localreplength
|
Ingeger. The index of a local variable in which to have the replacement length of the found data returned. Use 0 to not have the replacement length returned.
|
Return value
String. Returns the data string with the appropriate replacements performed.
Usage
Use this function for powerful text search and replace capabilities. The regular expression special characters supported are:
. |
Matches any character. |
\( |
This marks the start of a region for tagging a match. |
\) |
This marks the end of a tagged region. |
\n |
Where n is 1 through 9 refers to the first through ninth tagged region when replacing. For example, if the search string was Fred\([1-9]\)XXX and the replace string was Sam\1YYY, when applied to Fred2XXX this would generate Sam2YYY. |
\< |
This matches the start of a word. |
\> |
This matches the end of a word. |
\x |
This allows you to use a character x that would otherwise have a special meaning. For example, \[ would be interpreted as [ and not as the start of a character set. |
[...] |
This indicates a set of characters, for example, [abc] means any of the characters a, b or c. You can also use ranges, for example [a-z] for any lower case character. |
[^...] |
The complement of the characters in the set. For example, [^A-Za-z] means any character except an alphabetic character. |
^ |
This matches the start of a line (unless used inside a set, see above). |
$ |
This matches the end of a line. |
* |
This matches 0 or more times. For example, Sa*m matches Sm, Sam, Saam, Saaam and so on. |
+ |
This matches 1 or more times. For example, Sa+m matches Sam, Saam, Saaam and so on. |
An important note on this function is that it is a "greedy" regular expression search. When using the * and + special characters this function will not stop at the first match but will instead go to the last match.
This function will also not perform a regular expression search that spans multiple lines. If the data to search contains carraige returns or line feeds, the entire matching search data for the regular expression must exist within a single line. If your regular expression must span across a line, then add 2 to the flags to have CR's and LF's temporarily converted. CR will be converted to ASCII 128 and LF will be converted to ASCII 129. If you convert CF/LF then you can include them in your search with PowerHome escape characters ~128 and ~129 respectively.
You may also perform multiple searches by separating your search pattern using the PowerHome escape character ~255. This is most useful when CF/LF IS NOT replaced and trying to match a particular piece of data. If you do multiple searches, then replacement will only be performed on the last search.