Red Gregory

View Original

7 Ways To Start Using Regular Expressions In Notion Formulas

1. Remove Or Replace Multiple Characters

Simply use brackets around multiple characters you want to match. You can also replace all matched characters with a new character.

replaceAll(prop("Name"), "[NnTtSs]", "")

2. Extract Multiple Characters

To extract or isolate multiple matched characters in the formula box, add a “^” symbol inside the open bracket.

replaceAll(prop("Name"), "[^NnTtSs]", "")

Bonus: This strategy with the “^” can also be utilized to count the number of relations in a relation property or tags in a select property. Simply extract all commas in list, then find the length of the new string and add one.

if(not empty(prop("Tags")), length(replaceAll(prop("Tags"), "[^,]", "")) + 1, 0)

3. Remove Or Replace Multiple Words

Use the “|” symbol to match multiple words in the string. Replace those words with one new word or remove them altogether with a blank space.

replaceAll(prop("Name"), "Want|Replace|Words", "New")

You can also match case-sensitive words with this method.

replaceAll(prop("Name"), "Words|words", "…")

4. Remove Or Replace Last Character

To match the last character occurrence of any string, state “.$” alone like below.

replace(prop("Name"), ".$", "")

5. Extract Last Word After Space

To extract a last word or character after a space or defined character (ie.-), remove all before the last space with “.*[ ]”. See more about this at tip 7.

replaceAll(prop("Name"), ".*[ ]", "")

6. Remove All Letters Or Numbers

To remove all letters in a string, use [A-z], [A-Z] or [a-z]. To remove all numbers in a string, use [0-9]. Want to turn all extracted numbers into a number property? Nest the formula inside a toNumber(…) function.

replaceAll(prop("Name"), "[A-z]", "")

If you remove all letters or numbers and there is only an extra space at the front of the string to remove, at a “+ ” after [A-z] or [0-9].

replace(prop("Name"), "[0-9]+ ", "")

7. Remove All After Or Before Matched Character

Note: both replace() and replaceAll() functions can work here.

To remove all characters after a defined character or word, add a “.*” after the matched character or word.

replaceAll(prop("Name"), "In.*", "")

If you are removing all characters after a single defined character (ex. /), I like to nest the character in brackets.

replaceAll(prop("Url"), ".*[.]", "")

Bonus: Both strategies can be used to find a URL’s name. You can first remove all characters before “www.”. Next, all characters before “https://”. Lastly, all characters after “/”.

replaceAll(replaceAll(replaceAll(prop("Url"), ".*www.", ""), ".*https://", ""), "[/].*", "")

See this content in the original post

Further Reading

See this gallery in the original post