Giter Site home page Giter Site logo

phlak / splat Goto Github PK

View Code? Open in Web Editor NEW
108.0 3.0 5.0 100 KB

Glob-like file and pattern matching utility.

Home Page: https://packagist.org/packages/phlak/splat

License: MIT License

PHP 98.03% Makefile 1.97%
php glob regular-expression regex file-matching pattern-matching

splat's Introduction

Splat

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License GitHub branch checks state


Glob-like file and pattern matching utility.

Requirements

Installation

Install Splat with Composer.

composer require phlak/splat

Then import the Glob or Pattern classes as needed.

use PHLAK\Splat\Glob;
use PHLAK\Splat\Pattern;

Patterns

Glob methods accept a $pattern as the first parameter. This can be a string or an instance of \PHLAK\Splat\Pattern.

$pattern = new Pattern(...);
$pattern = Pattern::make(...);

A pattern string may contain one or more of the following special matching expressions.

Matching Expressions

  • ? matches any single character
  • * matches zero or more characters excluding / (\ on Windows)
  • ** matches zero or more characters including / (\ on Windows)
  • [abc] matches a single character from the set (i.e. a, b or c)
  • [a-c] matches a single character in the range (i.e. a, b or c)
  • [^abc] matches any character not in the set (i.e. not a, b or c)
  • [^a-c] matches any character not in the range (i.e. not a, b or c)
  • {foo,bar,baz} matches any pattern in the set (i.e. foo, bar or baz)
    • Sets may contain other matching patterns (i.e. {foo,ba[rz]})

Assertions

The following assertions can be use to assert that a string is followed by, or not followed by, another pattern.

  • (=foo) matches any string that also contains foo
  • (!foo) matches any string that does not also contain foo

For example, a pattern of *.tar(!.{gz,xz}) will match a string ending with .tar or .tar.bz but not tar.gz or tar.xz.

Converting Patterns To Regular Expressions

Glob patterns can be converted to a regular expression pattern.

Pattern::make('foo')->toRegex(); // Returns '#^foo$#'
Pattern::make('foo/bar.txt')->toRegex(); // Returns '#^foo/bar\.txt$#'
Pattern::make('file.{yml,yaml}')->toRegex(); // Returns '#^file\.(yml|yaml)$#'

You can control regular expression line anchors via the $options parameter.

Pattern::make('foo')->toRegex(Glob::NO_ANCHORS); // Returns '#foo#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR); // Returns '#^foo#'
Pattern::make('foo')->toRegex(Glob::END_ANCHOR); // Returns '#foo$#'
Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS); // Returns '#^foo$#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR | Glob::END_ANCHOR); // Returns '#^foo$#'

Pattern Character Escaping

Sometimes you may have characters in a string that shouldn't be treated as matching expression characters. In those situations you can escape any character by preceeding it with a backslash (\).

Pattern::make('What is happening\?');
Pattern::make('Wall-E \[2008\].mp4');

You may also escape glob pattern characters from a string programmatically with the Pattern::escape() method.

Pattern::escape('What is happening?'); // Returns 'What is happening\?'
Pattern::escape('*.{yml,yaml}'); // Returns '\*.\{yml\,yaml\}'
Pattern::escape('[Ss]pl*t.txt'); // Returns '\[Ss\]pl\*t.txt'

Methods

Files In

Get a list of files in a directory matching a glob pattern.

Glob::in('**.txt', 'some/file/path');

Returns a Symfony Finder Component containing the files matching the glob pattern within the specified directory (e.g. foo.txt, foo/bar.txt, foo/bar/baz.txt, etc.).


Exact Match

Test if a string matches a glob pattern.

Glob::match('*.txt', 'foo.txt'); // true
Glob::match('*.txt', 'foo.log'); // false

Match Start

Test if a string starts with a glob pattern.

Glob::matchStart('foo/*', 'foo/bar.txt'); // true
Glob::matchStart('foo/*', 'bar/foo.txt'); // false

Match End

Test if a string ends with a glob pattern.

Glob::matchEnd('**.txt', 'foo/bar.txt'); // true
Glob::matchEnd('**.txt', 'foo/bar.log'); // false

Match Within

Test if a string contains a glob pattern.

Glob::matchWithin('bar', 'foo/bar/baz.txt'); // true
Glob::matchWithin('bar', 'foo/baz/qux.txt'); // false

Filter an Array (of Strings)

Filter an array of strings to values matching a glob pattern.

Glob::filter('**.txt', [
    'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

// Returns ['foo.txt', 'foo/bar.txt']

Reject an Array (of Strings)

Filter an array of strings to values not matching a glob pattern.

Glob::reject('**.txt', [
    'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

// Returns ['foo', 'bar.zip', 'foo/bar.png']

Changelog

A list of changes can be found on the GitHub Releases page.

Troubleshooting

For general help and support join our GitHub Discussion or reach out on Twitter.

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is licensed under the MIT License.

splat's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar johanvanhelden avatar phlak avatar schakko avatar vlakoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.