Using regular expressions in formulas |
Regular expression can be used in the following formulas:
To use RE, each of these functions requires you to specify the SEARCH::RegEx (or 8192) flag/constant as its parameter as described in the Formula Composer window.
In the Find() function you can also use the SEARCH::RegExStr flag/constant. If it's specified, instead of the index the function returns the substringthat matches the pattern.
For example:
=find("\w\d{2,3}", "abc4567ghr", 1, SEARCH::RegEx) returns 3
=find("\w\d{2,3}", "abc4567ghr", 1, SEARCH::RegExStr) returns "c456"
Along with the SEARCH::RegExStr/SEARCH::RegEx options you can also specify which capturing group should be returned:
32 - 1st group, 64 - 2nd group, 128 - the 3rd group.
The example when this is helpful is extracting file folder, name or extension from the full file path:
To find/extract the extension:
=find("(\.[^.]+)$", "c:\file.txt", 1, 32+SEARCH::RegExStr)
To find/extract a folder from a file path:
=find("^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$", "c:\folder1\file.txt", 1, 32+SEARCH::RegExStr)
To find/extract the file name without the extension and path:
=find("^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$", "c:\file.txt", 1, 64+SEARCH::RegExStr)
Some other examples of regular expressions:
abc | finds cells containing (substrings) "abc" |
.bc | finds cells containing three character substrings consisting of any character followed by "bc" |
\Aabc | cell contents starting with "abc" |
abc\z | cell contents ending with "abc" |
\Aabc.*123\z | cell contents starting with "abc" and ending with "123" with any number of other characters in between |
abc\d\z | substrings ending with "abc" and one digit |
^a\d+ | cell contents containing a line that starts with "a" and at least one digit |
^a\d* | cell contents containing a line that starts with "a" and zero or more digits |
a\d*$ | cell contents containing a line that ends with "a" and zero or more digits |
[ab]+c | substrings "abc", "aabc", "abbabc" etc. and not "c" |
[ab]*c | substrings "abc", "aabc", "abbabc" etc. and "c" |
[^ab]+c | substrings ending with "c" and not containing "a" or "b" |
\w\d{2,3} | substrings consisting of one letter followed by 2 or 3 digits |
^ab\d{2,3}c | cell contents beginning with "ab", two or three digits and "c" |
abc|xyz | cells containing substrings "abc" or "xyz" |
For more information, see: PCRE regular expression syntax summary
The Replace() function additionally accept the replacing string which - if the SEARCH::RegEx flag is set - can contain:
Example 1.
Find pattern: | ab+ |
Database field content: | abcdefaabb |
Replace string: | x |
Replacement result: | xcdefax |
Example 2.
Find pattern: | (.)a+\d{1,3} |
Database field content: | abc aa0102 |
Replace string: | \1 |
Replacement result: | abc 2 |
Example 3.
Find pattern: | (ab) |
Database field content: | abcdef ghijk abb123 |
Replace string: | \u\1\l\1xyz\s |
Replacement result: | ABabxyz cdef ghijk ABabxyz b123 |
Example 4.
Find pattern: | \R |
Database field content: | abc def ghi |
Replace string: | \s |
Replacement result: | abc def ghi |