Skip to content

Instantly share code, notes, and snippets.

@retgef
Created September 18, 2013 21:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retgef/6615677 to your computer and use it in GitHub Desktop.
Save retgef/6615677 to your computer and use it in GitHub Desktop.
A basic, basic primer for regex and PHP.
_______ ______ __ _ ___ ___
|_ __ \ .' ____ \ [ | / |_ .' ..] .' ..]
| |__) | .---. .--./) .---. _ __ | (___ \_| .---. | |--. `| |-'__ _ _| |_ _| |_
| __ / / /__\\/ /'`\;/ /__\\[ \ [ ] _.____`. / /'`\] | .-. | | | [ | | |'-| |-''-| |-'
_| | \ \_| \__.,\ \._//| \__., > ' < | \____) || \__. | | | | | |, | \_/ |, | | | |
|____| |___|'.__.'.',__` '.__.'[__]`\_] \______.''.___.'[___]|__]\__/ '.__.'_/[___] [___]
( ( __))
---------------------------------------------------------------------------------------------------------------
/\
/ /
/ /
/ /
/ /
\/
/ - Delimiter to start and end an expression or can be used as a literal match but must be escaped \/
Example of delimiter:
preg_match('/foobar/', 'foobar') - returns true
Example of escaped literal:
preg_match('/\//', '/') - returns true
---------------------------------------------------------------------------------------------------------------
__ __
.' _| |_ `.
| | | |
| | | |
| |_ _| |
`.__| |__,'
() - Parentheses - supplies a regex function with an array of sub-matches - can be nested infinately
preg_match_all('/foo(bar)/', 'foobar', $matches) - returns an array $matches with the value of 'bar'
preg_match_all('/foo(ba(r))/', 'foobar', $matches) - returns an array $matches with the value of 'bar' & 'r'
---------------------------------------------------------------------------------------------------------------
___ ___
| _| |_ |
| | | |
| | | |
| |_ _| |
|___| |___|
[] - Square Brackets - creates a list of things to match i.e. A-Za-z0-9 or any character (escaped if need be)
Example of matching integers:
preg_match('/foo[0-5]bar/', 'foo3bar') - returns true
Example of matching range of alpha chars:
preg_match('/foo[A-D]bar/', 'fooCbar') - returns true
---------------------------------------------------------------------------------------------------------------
.-.
__| |__
|__ __|
| |
'-'
+ - Plus - matches a preceeding character 1 or more times
preg_match('/fo+bar/', 'foobar') - returns true
---------------------------------------------------------------------------------------------------------------
_____
/ ___ `.
|_/___) |
/ __.'
|_|
(_)
? - Matches preceeding character 0 or 1 times.
preg_match('/foobar?/', 'fooba') - returns true
---------------------------------------------------------------------------------------------------------------
__ __
.' _/ \_ `.
| | | |
< < > >
| |_ _| |
`.__\ /__.'
{} - Curly Brackets - Lets you define a range of n times. i.e. {2} {2,6} (N times between 2 and 6)
preg_match('/fo{2}b[ar]{1,2}', 'foobar') OR preg_match('/fo{2}b[ar]{1,2}', 'fooba') - returns true
---------------------------------------------------------------------------------------------------------------
_
| |
| |
| |
| |
|_|
| - Pipe - Search for a list of specific strings
preg_match('/foo(blue|clear|red)bar/', 'fooclearbar') - returns true
---------------------------------------------------------------------------------------------------------------
___
/ _ \
|_/ \_|
^ - Caret - Start of line or negation of expression (everything except)
Example of start of line:
preg_match('/^foobar/', 'foobar') - returns true
Example of expression negation:
preg_match('/[^foobar]{6} I Love You/', 'foobar I Love You') - returns false
preg_match('/[foobar]{6} I Love You/', 'foobar I Love You') - returns true
---------------------------------------------------------------------------------------------------------------
,--.
,-| |-.
| | |-'
'-| |`\
.--| | /
`--| |'
`--'
$ - Dollar Sign - look at the end of a line
preg_match('/^foobar$/', 'foobar') - returns true
---------------------------------------------------------------------------------------------------------------
_
(_)
. - Matches any character
preg_match('/^.+$/', 'foobar') - returns true (all chars between BOL and EOL)
---------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment