Base - Extract substring from file name

I’m trying to extract a substring from a file name using base formulas. The substing is a date in the format yyyy-MM-dd, so I can easily match it with the regex /\d{4}-\d{2}-\d{2}/.

Looking into the base documentation I’ve found the matches() function, which only tells me if the string matches the pattern.

To extract the pattern I was thinking to use replace() with regex lookahead/lookbehind, but it’s quickly turning me crazy

I once tried to achieve something similar, but ended up doing it differently because I could find a good solution.

There is currently no function that returns a matching string. With the replace() function you are also limited as replacementdoes only allow for a string, but not regex, so no access to capture groups.

A possible workaround can be the convert functions such as number() or date(). If you before use replace() function to remove all non date characters, it might work depending on your specific case.

Yeah that it’s what I’m trying to do. I’m thinking something like:

  • replace everything before the date pattern with empty string
  • replace everything after the date pattern with empty string
  • convert that to date with date()

When I manage to find some time do to this I’ll post it there for everyone else

Apparently with replace you can reference capture groups

date(file.name.replace(/.*?(\d{4}-\d{2}-\d{2}).*/, "$1"))
1 Like

Interesting. I have never tried as the documentation only mentions to allow strings. I will test it.

EDIT: Capture groups actually do work with the replace() function. Thanks for pointing this out to me.

To be fair, props to Claude. At the beginning it allucinated with some non existing functions, but also suggested me to use capture groups.

I’m guessing under the hood many base functions are just wrappers to regular js functions. Being not documented I hope it’s not a bug that will be solved sooner or later