Note: Most of this info is available directly from the Zend Website, there are a couple of original additions but most of it is ripped with comments.
preg_match( '/apple/', 'I saw the apple tree.' );
preg_match( '/a..le/', 'I saw the apple tree' );
preg_match( '/^apple/', 'I saw the apple tree.' );
preg_match( '/apple$/', 'I saw the apple tree.' );
preg_match( '/appleX?/', 'I saw the apple tree.' );
preg_match( '/ap*le/', 'I saw the apple tree.' );
preg_match( '/ap+le/', 'I saw the apple tree.' );
preg_match( '/ap{2}le/', 'I saw the apple tree.' );
preg_match( '/ap{2,4}le/', 'I saw the apple tree.' );
preg_match( '/(apple|orange)/', 'I saw the apple tree.' );
preg_match( '/apple[0-9m-z]/', 'I saw the apples tree.' );
preg_match( '/ap*?le/', 'I saw the apple tree.' );
Escaping also needs to be used when using any of the meta characters mentioned above, you can't just match '1 + 2' with '/1 + 2/', you have to escape the '+'. So use '1 \+ 2'. Remember that as '\' denotes an escape sequence, it too needs escaping '\\'.
$string = 'how now, brown cow?'; $string = preg_replace( '/^(.+)\snow, (.+)$/', 'first submatch: "\\1", second submatch: "\\2"', $string ); echo $string;
$string = 'how now, brown cow?'; preg_match( '/^(.+)\snow, (.+)$/', $string, $matches ); print_r( $matches );
Array
(
[0] => how now, brown cow?
[1] => how
[2] => brown cow?
)