Using Regular Expressions in Formulas

1. Overview

Regular expressions can be used in:

To enable regular expressions, specify the SEARCH::RegEx flag (value 8192) as described in the Formula Composer window.

In Find(), you may also use SEARCH::RegExStr. If used, the function returns the matching substring instead of the index.

Examples:

=find("\w\d{2,3}", "abc4567ghr", 1, SEARCH::RegEx) → 3

=find("\w\d{2,3}", "abc4567ghr", 1, SEARCH::RegExStr) → "c456"

2. Returning Capturing Groups

You can specify which capturing group should be returned:

Useful for extracting file components:

Extension:
=find("(\.[^.]+)$", "c:\file.txt", 1, 32+SEARCH::RegExStr)

Folder:
=find("^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$", "c:\folder1\file.txt", 1, 32+SEARCH::RegExStr)

File name without extension:
=find("^(.*[\\\/])?(\.*.*?)(\.[^.]+?|)$", "c:\file.txt", 1, 64+SEARCH::RegExStr)

3. Regular Expression Examples

abcCells containing “abc”
.bcAny character + “bc”
\AabcStarts with “abc”
abc\zEnds with “abc”
\Aabc.*123\zStarts with “abc”, ends with “123”
abc\d\zEnds with “abc” + digit
^a\d+Line starting with “a” + digits
^a\d*Line starting with “a” + optional digits
a\d*$Line ending with “a” + optional digits
[ab]+c“abc”, “aabc”, “abbabc”… but not “c” alone
[ab]*cSame as above, including “c”
[^ab]+cEnds with “c”, contains no “a” or “b”
\w\d{2,3}Letter + 2–3 digits
^ab\d{2,3}cStarts with “ab”, 2–3 digits, “c”
abc|xyzContains “abc” or “xyz”

See PCRE regular expression syntax summary.

4. Replace() with Regular Expressions

If SEARCH::RegEx is set, the replace string may contain:

  1. Capturing group references: \1, \2, …
  2. \l, \L, \u, \U — case conversion
  3. \r, \n — line feed / newline
  4. \s — space

5. Examples

Example 1

Find:ab+
Content:abcdefaabb
Replace:x
Result:xcdefax

Example 2

Find:(.)a+\d{1,3}
Content:abc aa0102
Replace:\1
Result:abc 2

Example 3

Find:(ab)
Content:abcdef ghijk abb123
Replace:\u\1\l\1xyz\s
Result:ABabxyz cdef ghijk ABabxyz b123

Example 4

Find:\R
Content:abc
def
ghi
Replace:\s
Result:abc def ghi