Core\Path::matches
Core\Path::matches(string $pattern, string $path): boolCore\Path::matches( string $pattern, string $path, ): bool
Checks whether a path matches a pattern such as *.jpg. It never looks at the disk.
A part of a path is a folder or file name between two /. A pattern with no / is compared with the last part of the path, in any folder. So *.jpg matches photos/2026/lake.jpg. A pattern with a / is compared with the whole path, from its start. A / at the start of the pattern does the same. So /build matches the path build and not src/build. / and \ are both separators on every platform.
*matches any text inside one part, and?matches one character.**alone between two/matches any number of folders, or none.[a-z]matches one character in the set, and[!a-z]one character outside it.{jpg,png}matches one of the words in the braces.
To match a special character such as *, write it in brackets: [*]. The comparison is case-sensitive on every platform. A pattern that is not valid throws a RuntimeError that names the position of the problem.
The examples below match a file name, match a folder with **, and match sets with brackets and braces.
Parameters
$patternstringThe pattern. A part is a folder or file name between two separators.*matches any text inside one part, and?matches one character.**alone in a part matches any number of folders, or none.[abc]matches one of the listed characters,[a-z]one in the range, and[!abc]one that is not listed.{jpg,png}matches one of the words. A pattern without a separator is compared with the file name only. So*.jpgmatchesphotos/2024/a.jpg. A pattern with a separator is compared from the first part, sosrc/*.nvsdoes not matchlib/src/a.nvs. A pattern that starts with a separator, such as/build, matchesbuildand notsrc/build. To match*,?,[or{itself, write it in brackets, such as[*].$pathstringThe path to check./and\are both separators on every platform. The method ignores a/or a drive such asC:\at the start of the path.
Return value
Returns bool. true when the path matches the pattern. Upper and lower case are different, so *.jpg does not match A.JPG. *.{jpg,JPG} matches both.
Errors
RuntimeErrorThe pattern is not valid, for example a[without its]. The message gives the position of the problem.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Path::matches($pattern, $path) checks whether a path matches a pattern such as *.jpg or src/**/*.nvs. The syntax is the same on every system, and the comparison is case-sensitive. It has *, ?, **, sets in brackets such as [a-z] and choices in braces such as {jpg,png}. |
Examples
Match a file name
<?nvs// Shows how to check whether a file name matches a pattern such as `*.jpg`.
array<string> $files = [ 'photos/beach.jpg', 'photos/2026/summer/lake.jpg', 'photos/notes.txt', 'beach.jpg.bak', 'photos/CAMERA.JPG',];
// A pattern with no `/` is compared with the file name, at any depth. Case matters, so `CAMERA.JPG` does not match.// This prints `photos/beach.jpg` and `photos/2026/summer/lake.jpg`.foreach ($files as string $file) { if (Core\Path::matches('*.jpg', $file)) { echo $file, "\n"; }}
// `?` matches exactly one character. This prints `yes`, then `no`.echo Core\Path::matches('lake.?pg', 'photos/lake.jpg') ? 'yes' : 'no', "\n";echo Core\Path::matches('lake.?pg', 'photos/lake.pg') ? 'yes' : 'no', "\n";photos/beach.jpgphotos/2026/summer/lake.jpgyesnoSkip a folder with double star
<?nvs// Shows how to use `**` to match a folder and everything inside it.
array<string> $files = [ 'src/app.nvs', 'src/build/app.js', 'src/build/maps/app.js.map', 'build/index.html', 'docs/build.md',];
// A pattern with a `/` is compared from the start. `**` matches any number of folders.// This prints `src/build/app.js` and `src/build/maps/app.js.map`.foreach ($files as string $file) { if (Core\Path::matches('src/build/**', $file)) { echo $file, "\n"; }}
// A part is a folder or file name between two `/`. `**` can also be between two parts.// This prints `yes`.echo Core\Path::matches('src/**/*.map', 'src/build/maps/app.js.map') ? 'yes' : 'no', "\n";
// `/build` matches `build` at the start of the path, and not `src/build`.// This prints `yes`, then `no`.echo Core\Path::matches('/build', 'build') ? 'yes' : 'no', "\n";echo Core\Path::matches('/build', 'src/build') ? 'yes' : 'no', "\n";src/build/app.jssrc/build/maps/app.js.mapyesyesnoSets in brackets and braces
<?nvs// Shows how to match one of several characters with brackets `[ ]`, and one of several words with braces `{ }`.
array<string> $files = [ 'report-1.csv', 'report-7.csv', 'report-x.csv', 'report-7.pdf', 'report-7.PDF',];
// `[0-9]` matches one digit. `{csv,pdf,PDF}` matches one of the three words.// This prints `report-1.csv`, `report-7.csv`, `report-7.pdf` and `report-7.PDF`.foreach ($files as string $file) { if (Core\Path::matches('report-[0-9].{csv,pdf,PDF}', $file)) { echo $file, "\n"; }}
// `[!0-9]` matches one character that is not a digit. This prints `report-x.csv`.foreach ($files as string $file) { if (Core\Path::matches('report-[!0-9].csv', $file)) { echo $file, "\n"; }}
// A special character in brackets matches only that character, so `[*]` matches `*`.// This prints `yes`.echo Core\Path::matches('notes[*].txt', 'notes*.txt') ? 'yes' : 'no', "\n";
// A pattern that is not valid throws a `RuntimeError`. Its message names the position.// The `[` is character 8 of the pattern. This prints that it has no closing `]`.try { Core\Path::matches('report-[0-9.csv', 'report-1.csv');} catch (RuntimeError $error) { echo $error->message, "\n";}report-1.csvreport-7.csvreport-7.pdfreport-7.PDFreport-x.csvyesCore\Path::matches(): the pattern `report-[0-9.csv` is not valid. At character 8, `[` has no closing `]`.