Regex in find(), filterBy() or file()?

Is it possible to use regexp to find a file with one of these functions : find(), filterBy() or file() (or another one) ?

for the moment I have :

<?php if($cvfiles = $page->files()->filterBy('extension', 'pdf')->filterBy('filename', '*=', 'cv')->filterBy('filename', '*=', 'dl')): ?>

but maybe there is a simpler way to do it with regex and/or joker characters like *

Thank You

You can use a filter with callback: https://getkirby.com/docs/cheatsheet/files/filter

1 Like

Like this ?

$cvfiles = $page->files()->filter(function($image) {
  return $cvfile->filename() == preg_match("myregexp", $cvfile->filename());
});

Do I need a foreach after that ?

Yes, $cvfiles a normal filtered collection.

1 Like

Well, almost. It has to be like this:

$cvfiles = $page->files()->filter(function($image) {
  return preg_match('/myregexp/', $cvfile->filename());
});
1 Like