https://github.com/drgomesp/greppy
:scroll: Relax with this awesome library for working with regular expressions with PHP
https://github.com/drgomesp/greppy
Last synced: 18 days ago
JSON representation
:scroll: Relax with this awesome library for working with regular expressions with PHP
- Host: GitHub
- URL: https://github.com/drgomesp/greppy
- Owner: drgomesp
- License: mit
- Created: 2013-03-05T20:09:54.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2016-03-20T10:58:12.000Z (about 9 years ago)
- Last Synced: 2025-04-09T04:26:10.551Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 47.9 KB
- Stars: 90
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Greppy
==================
>## Deprecation Notice>Greppy is going to be phased away in favor of the [PHPVerbalExpressions](https://github.com/VerbalExpressions/PHPVerbalExpressions) project.
[](https://travis-ci.org/drgomesp/greppy)
[](https://scrutinizer-ci.com/g/drgomesp/Greppy/)
[](https://packagist.org/packages/relaxphp/greppy)
[](https://packagist.org/packages/relaxphp/greppy)
[](https://insight.sensiolabs.com/projects/4aec493b-b7f3-4e43-8412-361b84a32c6f/mini.png)Why use Greppy?
-------------
- Isolate your regex patterns and matching so they can be easily mocked inside unit tests
- Represent important and recurrent patterns with custom pattern classes
- Write more human-readable regular expressions with a fluent API using the `FluentPattern` object.Feature Guide
-------------### Bootstrap
```php
$p = new Relax\Greppy\Pattern();
```### Custom pattern objects
With Greppy, you can define pattern objects – types – to easily define, reuse and maintain common
patterns used in web applications.If you use regex to match domain, for instance, instead of doing:
```php
preg_match("/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i", $subject);
```You may define a `DomainPattern` type, such as:
```php
namespace Your\Namespace;use Relax\Greppy\Pattern;
class DomainPattern implements Pattern
{
public function __toString()
{
return "/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/";
}
}
```And use it like this:
```php
$domain = new Your\Namespace\DomainPattern();
$m = new Relax\Greppy\SimpleMatcher("http://www.google.com");
$m->caseless()->matches($domain); // true
```### The predefined Pattern object
#### Matching any single character
The PHP way:
```php
preg_match("/./", "any"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("any");
$m->matches($p->any()); // true
```#### Matching any digit
The PHP way:
```php
preg_match("/\d/", "5"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("5");
$m->matches($p->digit()); // true
```#### Matching a literal
The PHP way:
```php
preg_match("/e/", "hey"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("hey");
$m->matches($p->literal("e")); // true
```#### Matching a group of literals
The PHP way:
```php
preg_match("/[abc]/", "anthem"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("anthem");
$m->matches($p->literal("a", "b", "c")); // true
```#### Matching a range
The PHP way:
```php
preg_match("/[a-z]/", "any"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("any");
$m->matches($p->range("a", "z")); // true
```#### Matching a repetition
The PHP way:
```php
preg_match("/z{3}/", "wazzzup"); // 1
preg_match("/z{2,4}/", "wazzzzup"); // 1
```
The Greppy way:
```php
$m = new Relax\Greppy\SimpleMatcher("wazzzup");
$m->matches($p->repetition("z", 3)); // true$m = new Relax\Greppy\SimpleMatcher("wazzzzup");
$m->matches($p->repetition("z", 2, 4)); // true
```