How To Extract List Items With A Notion Formula

list-item-formula-notion-tip.png

🛠 Formula For Lists

How to locate and extract items from a list in Notion. Here are some formulas and pointers on how to use Notion’s replace() function with regular expressions to return specified items in a multi-select, text or rollup property. The formulas work for lists separated by a comma, however, you can make small changes to extract lists separated by other characters. Here’s how it works.

What Is The Replace() Function?

replace-function-notion-formula.png

Use replace() or replaceAll() to replace a character or set of characters in a string with a new value. You can also use this function to remove characters from a string all together.

The syntax: replace(prop(“Name of property with string“), “match character(s) in string“, “replace matched character(s) with this“)

  • replace(prop(“Name”), ““, ““) or replaceAll(prop(“Name”), ““, ““)

The replace() function replaces the first instance of matched character(s).

The replaceAll() function replaces all matched character(s).

Count List Items Formula

Count specified items in rollup, text, or multi-select properties. (ie. count items in rollup/multi-select property called High Impact).

count-list-items-tags-multi-select-notion-formula.png

The matched character below is Low Impact. Replace "Low Impact" with phrase you want to count.

A string property can be Text, Multi-Select, or a Rollup.

length(replaceAll(replaceAll(prop("String Property Name"), "Low Impact", "§"), "[^§]", ""))

Logic Behind The Solution

  1. Replace all instances of matched character with a unique symbol.

    • length(replaceAll(replaceAll(prop("String Property Name"), "Low Impact", "§"), "[^§]", ""))

  2. Replace all that is NOT the unique symbol with an empty space.

    • length(replaceAll(replaceAll(prop("String Property Name"), "Low Impact", "§"), "[^§]", ""))

  3. Find the length of the unique symbols.

    • length(replaceAll(replaceAll(prop("String Property Name"), "Low Impact", "§"), "[^§]", ""))

Extract List Items Formula

Extract 1st, 2nd, 3rd, 4th, 5th ... last, ect. items in a list.

notion-formula-extract-list-items-regular-expression.png

These formulas apply to lists separated by commas. Is your list separated by characters that aren't commas? Switch anywhere you see a comma with your special character.

  • 0 = first item

    • 1 = second item

      • 2 = third item

Extract Item From List String

replaceAll(replace(prop("Name of Property"), "(?:[^,]*,){2}", ""), ",.*", "")

Extract Item From List String (Multi-Select or Text with extra space)

What is the difference between these formulas? The extra space will be removed after every comma and before every item. If your solution, for example, has a pesky extra space before the item, use this formula to remove the space.

This may only be necessary to use in some cases.

replaceAll(replace(prop("Name of Property"), "(?:[^,]*, ){2}", ""), ",.*", "")

 

Further Reading