Skip to content

Instantly share code, notes, and snippets.

@alganet
Created March 6, 2013 19:50
Show Gist options
  • Save alganet/5102452 to your computer and use it in GitHub Desktop.
Save alganet/5102452 to your computer and use it in GitHub Desktop.
Greppy
<?php
// Matches a single char. Equivalent to: /./
Pattern::char()->test('a'); //true
Pattern::char()->replace('a', 'b'); //b
Pattern::char()->match('a'); //array('a') (captures groups)
// Possible "steps":
Pattern::char();
Pattern::char('A-Z'); // Range [A-Z]
Pattern::char('a', 'b', 'c-d'); // List [abc-d]
Pattern::none('a'); // [^a] (same as char but negating)
Pattern::grouping(
Pattern::char('a'),
Pattern::char('b')
); // ([a]|[b])
Pattern::hidden(
Pattern::char('a'),
Pattern::char('b')
); // (?:[a]|[b])
Pattern::grouping('a', 'b'); // Same as above
Pattern::digit()->count('*'); // \d*
Pattern::digit()->count(1); // \d{1}
Pattern::digit()->count(2,3); // \d{2,3}
Pattern::digit()->max(3); // \d{,3}
Pattern::digit()->min(2); // \d{2,}
Pattern::beginLine()->char()->count('*')->endLine(); // ^.*$
Pattern::line(Pattern::char()->count('*')); // ^.*$
@drgomesp
Copy link

drgomesp commented Mar 6, 2013

I'm not sure if I would expose an API for the preg_ related functions.

Pattern::char()->test('a');         //true
Pattern::char()->replace('a', 'b'); //b
Pattern::char()->match('a');        //array('a') (captures groups)

Maybe using it the usual way looks more verbose:

preg_match(Pattern::char());

About this:

Pattern::digit()->count('*');
Pattern::digit()->count(1); 

I think a method for the actual intent is more verbose:

Pattern::digit()->any();
Pattern::digit()->exactly(1); 

This:

Pattern::beginLine()->char()->count('*')->endLine(); // ^.*$
Pattern::line(Pattern::char()->count('*')); // ^.*$

I prefer the first way, it's definitely more fluent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment