Running php version 5.3.3
preg_match( '/abcd/', 'abcd') = 1
preg_match_all( '/abcd/', 'abcd', Array ) = 1
preg_match( '/abcd/', 'azcd') = 0
preg_match_all( '/abcd/', 'azcd', Array ) = 0
preg_match( '/a..d/', 'azcd') = 1
preg_match_all( '/a..d/', 'azcd', Array ) = 1
preg_match( '/ab*cd/', 'abbbbcd') = 1
preg_match_all( '/ab*cd/', 'abbbbcd', Array ) = 1
preg_match( '/ab*cd/', 'abcd') = 1
preg_match_all( '/ab*cd/', 'abcd', Array ) = 1
preg_match( '/ab*cd/', 'acd') = 1
preg_match_all( '/ab*cd/', 'acd', Array ) = 1
preg_match( '/ab+cd/', 'abbbbcd') = 1
preg_match_all( '/ab+cd/', 'abbbbcd', Array ) = 1
preg_match( '/ab+cd/', 'abcd') = 1
preg_match_all( '/ab+cd/', 'abcd', Array ) = 1
preg_match( '/ab+cd/', 'acd') = 0
preg_match_all( '/ab+cd/', 'acd', Array ) = 0
preg_match( '/ab.*cd/', 'abcd') = 1
preg_match_all( '/ab.*cd/', 'abcd', Array ) = 1
preg_match( '/ab.*cd/', 'abXd') = 0
preg_match_all( '/ab.*cd/', 'abXd', Array ) = 0
preg_match( '/ab.*cd/', 'abBlahBlahBlahcd') = 1
preg_match_all( '/ab.*cd/', 'abBlahBlahBlahcd', Array ) = 1
preg_match( '/ab.+cd/', 'abcd') = 0
preg_match_all( '/ab.+cd/', 'abcd', Array ) = 0
preg_match( '/ab.+cd/', 'abXcd') = 1
preg_match_all( '/ab.+cd/', 'abXcd', Array ) = 1
preg_match( '/ab.+cd/', 'abBlahBlahBlahcd') = 1
preg_match_all( '/ab.+cd/', 'abBlahBlahBlahcd', Array ) = 1
preg_match( '/ab?cd/', 'acd') = 1
preg_match_all( '/ab?cd/', 'acd', Array ) = 1
preg_match( '/ab?cd/', 'abcd') = 1
preg_match_all( '/ab?cd/', 'abcd', Array ) = 1
preg_match( '/ab?cd/', 'abbbbcd') = 0
preg_match_all( '/ab?cd/', 'abbbbcd', Array ) = 0
preg_match( '/(ab)*cd/', 'ababababcd') = 1
preg_match_all( '/(ab)*cd/', 'ababababcd', Array ) = 1
preg_match( '/(ab)*cd/', 'abbbbcd') = 1
preg_match_all( '/(ab)*cd/', 'abbbbcd', Array ) = 1
preg_match( '/row/', 'How now, brown cow?') = 1
preg_match_all( '/row/', 'How now, brown cow?', Array ) = 1
preg_match( '/.ow/', 'How now, brown cow?') = 1
preg_match_all( '/.ow/', 'How now, brown cow?', Array ) = 4
preg_match( '/^.ow$/', 'How now, brown cow?') = 0
preg_match_all( '/^.ow$/', 'How now, brown cow?', Array ) = 0
preg_match( '/^.ow$/', 'Zow') = 1
preg_match_all( '/^.ow$/', 'Zow', Array ) = 1
preg_match( '/^.ow/', 'Zowee') = 1
preg_match_all( '/^.ow/', 'Zowee', Array ) = 1
preg_match( '/w.e$/', 'Wowee Zowee') = 1
preg_match_all( '/w.e$/', 'Wowee Zowee', Array ) = 1
preg_match( '/[WZ]ow/', 'Wowee Zowee') = 1
preg_match_all( '/[WZ]ow/', 'Wowee Zowee', Array ) = 2
preg_match( '/[WZ]ow/', 'Yow') = 0
preg_match_all( '/[WZ]ow/', 'Yow', Array ) = 0
preg_match( '/[W-Z]ow/', 'Yowee') = 1
preg_match_all( '/[W-Z]ow/', 'Yowee', Array ) = 1
preg_match( '/ab[0-9]+de/', 'ab789de') = 1
preg_match_all( '/ab[0-9]+de/', 'ab789de', Array ) = 1
preg_match( '/[0-9]*/', '00047') = 1
preg_match_all( '/[0-9]*/', '00047', Array ) = 2
preg_match( '/n[oe]w/', 'Hew new, brown cow?') = 1
preg_match_all( '/n[oe]w/', 'Hew new, brown cow?', Array ) = 1
preg_match( '/n[oe]w/', 'Haw naw, brawn caw?') = 0
preg_match_all( '/n[oe]w/', 'Haw naw, brawn caw?', Array ) = 0
preg_match( '/rows?/', 'How now, browzing cow?') = 1
preg_match_all( '/rows?/', 'How now, browzing cow?', Array ) = 1
preg_match( '/rows?/', 'Hews news, brewns cews?') = 0
preg_match_all( '/rows?/', 'Hews news, brewns cews?', Array ) = 0
preg_match( '/rows?/', ''Hew new, brewn crew?', he crows.') = 1
preg_match_all( '/rows?/', ''Hew new, brewn crew?', he crows.', Array ) = 1
preg_match( '/[0-9]*/', '00047') = 1
preg_match_all( '/[0-9]*/', '00047', Array ) = 2
preg_match( '/\d*/', '00047') = 1
preg_match_all( '/\d*/', '00047', Array ) = 2
preg_match( '/[0-9]+/', '00047') = 1
preg_match_all( '/[0-9]+/', '00047', Array ) = 1
preg_match( '/^[0-9]*$/', '00047') = 1
preg_match_all( '/^[0-9]*$/', '00047', Array ) = 1
preg_match( '/^[0-9]*$/', '00047abc') = 0
preg_match_all( '/^[0-9]*$/', '00047abc', Array ) = 0
preg_match( '/^[0-9]*/', '00047abc') = 1
preg_match_all( '/^[0-9]*/', '00047abc', Array ) = 1
preg_match( '/^[0-9]*/', 'abc00047') = 1
preg_match_all( '/^[0-9]*/', 'abc00047', Array ) = 1
preg_match( '/\d*/', '00047abc') = 1
preg_match_all( '/\d*/', '00047abc', Array ) = 5
preg_match( '/[^0-9]*/', '00047abc') = 1
preg_match_all( '/[^0-9]*/', '00047abc', Array ) = 7
preg_match( '/[^0-9]+/', '00047abc') = 1
preg_match_all( '/[^0-9]+/', '00047abc', Array ) = 1
Array
(
[0] => Array
(
[0] => ow
[1] => ow
[2] => ow
[3] => ow
)
)
parentheses, and backreferences:
preg_match( '/abc/', 'xyzabcdef') = 1
preg_match_all( '/abc/', 'xyzabcdef', Array ) = 1
preg_match( '/(a)b(c)/', 'xyzabcdef') = 1
preg_match_all( '/(a)b(c)/', 'xyzabcdef', Array ) = 1
preg_match( '/(a)b\$1/', 'xyzabcdef') = 0
preg_match_all( '/(a)b\$1/', 'xyzabcdef', Array ) = 0
preg_match( '/(a)b\$1/', 'xyzabadef') = 0
preg_match_all( '/(a)b\$1/', 'xyzabadef', Array ) = 0
whitespace:
preg_match( '/\p{Z}/', '
') = 1
preg_match_all( '/\p{Z}/', '
', Array ) = 1
preg_match( '/[[:space:]]/', '
') = 1
preg_match_all( '/[[:space:]]/', '
', Array ) = 4
calling preg_replace
:
xyz!!!def
xyz!!!def
xyz(I see a and c)def
xyz(I see a and c)def
xyzabadef
Source
<?php error_reporting ( E_ALL | E_STRICT ); ini_set ( 'display_errors' , 'stderr' ); ini_set ( 'display_startup_errors' , true ); date_default_timezone_set ( 'America/New_York' ); ?> <html> <head> <?php /* Look up an index in an array, * returning the default value if not present. */ function safeLookup ( $arr , $index , $default = false ) { return ( array_key_exists ( $index , $arr )) ? $arr [ $index ] : $default ; } /* Look up an item from the input (POST or GET), * returning the default value if not present. * (Also unmangle: remove any inserted-magic-quotes, and trim.) */ function getInput ( $index , $default = false ) { return unmangleInput ( safeLookup ( $_POST , $index , ( safeLookup ( $_GET , $index , $default )) ) ); } /* Given a string from user-input, * remove any slashes that magic-quotes might have added, * and trim. Otherwise, leave it unchanged. * * Given an array, recursively unmangle all strings it contains. */ function unmangleInput ( $val ) { if ( is_string ( $val )) { return trim ( get_magic_quotes_gpc () ? stripslashes ( $val ) : $val ); } else if ( is_array ( $val )) { return array_map ( "unmangleInput" , $val ); } else { echo ">>> unmangleInput: unhandled case, " ; var_dump ( $val ); echo "\n" ; return $val ; } } /* Take a string of raw data, and sanitize it for rendering in HTML. */ function rawStringToHTML ( $str ) { return nl2br ( htmlspecialchars ( $str )); } ?> </head> <body> <?php echo "Running php version " , phpversion (); ?> <?php $NUMCOLS = 3 ; $NUMROWS = 3 ; ?> <form action="lect16-regexps-examples.php" method="GET"/> <table> <tr><th></th><th></th><th colspan="<?php echo $NUMCOLS ; ?> ">strings:</th></tr> <tr> <th></th><th></th> <?php /* column heads: input forms iwth name=strs[], sticky, requires numeric indices [0,NUMCOLS) : */ $strs = getInput ( "strs" , array() ); for ( $i = 0 ; $i < $NUMCOLS ; ++ $i ) { echo " <th><input name='strs[]' value='" . rawStringToHTML ( safeLookup ( $strs , $i , "" ) ) . "' width=20/></th>\n" ; } ?> </tr> <?php /* regular rows: a pattern from pat[], followed by NUMCOLS calls to preg_match_all */ $pats = getInput ( "pats" , array() ); for ( $j = 0 ; $j < $NUMROWS ; ++ $j ) { echo " <tr>\n" ; if ( $j === 0 ) echo "<th rowspan=' $NUMROWS '>patterns:</th>\n" ; //echo " <th>". ($j===0 ? "patterns:": "") ."</th>\n"; echo " <th><input name='pats[]' value='" . rawStringToHTML ( safeLookup ( $pats , $j , "" ) ) . "' width=20/></th>\n" ; for ( $i = 0 ; $i < $NUMCOLS ; ++ $i ) { echo " <td>" // . "::$pats[$j]:$strs[$i]::" . ( safeLookup ( $pats , $j ) ? ( preg_match_all ( safeLookup ( $pats , $j , "" ), safeLookup ( $strs , $i , "" ), $ignore )) : "n/a" ) . "</td>\n" ; } echo " </tr>\n" ; } ?> </table> <input type="submit" value="Submit"> <br/> <?php function demoMatch ( $pat , $target ) { $matchResults = array(); echo "<pre>preg_match( '" , $pat , "', '" , $target , "') = " , preg_match ( $pat , $target ), "</pre>" ; echo "<br />\n" ; echo "<pre>preg_match_all( '" , $pat , "', '" , $target , "', $matchResults ) = " , preg_match_all ( $pat , $target , $matchResults ), "</pre>" ; echo "<br />\n" ; echo "<br />\n" ; } demoMatch ( '/abcd/' , 'abcd' ); demoMatch ( '/abcd/' , 'azcd' ); // false demoMatch ( '/a..d/' , 'azcd' ); // . matches any single character (besides newline, null) demoMatch ( '/ab*cd/' , 'abbbbcd' ); // b* matches 0-or-more-b's. demoMatch ( '/ab*cd/' , 'abcd' ); demoMatch ( '/ab*cd/' , 'acd' ); demoMatch ( '/ab+cd/' , 'abbbbcd' ); // b+ matches 1-or-more-b's. demoMatch ( '/ab+cd/' , 'abcd' ); demoMatch ( '/ab+cd/' , 'acd' ); // false demoMatch ( '/ab.*cd/' , 'abcd' ); demoMatch ( '/ab.*cd/' , 'abXd' ); // .* and .+ are a common patterns. demoMatch ( '/ab.*cd/' , 'abBlahBlahBlahcd' ); demoMatch ( '/ab.+cd/' , 'abcd' ); // false demoMatch ( '/ab.+cd/' , 'abXcd' ); demoMatch ( '/ab.+cd/' , 'abBlahBlahBlahcd' ); // false demoMatch ( '/ab?cd/' , 'acd' ); // b? matches 0-or-1 b demoMatch ( '/ab?cd/' , 'abcd' ); demoMatch ( '/ab?cd/' , 'abbbbcd' ); // false demoMatch ( '/(ab)*cd/' , 'ababababcd' ); // parens do grouping demoMatch ( '/(ab)*cd/' , 'abbbbcd' ); // false // WARNING: preg_match looks to see if the string *contains* a match! demoMatch ( '/row/' , 'How now, brown cow?' ); // true demoMatch ( '/.ow/' , 'How now, brown cow?' ); // true // Use "^" to specify the start-of-string, and "$" to specify end-of-string. demoMatch ( '/^.ow$/' , 'How now, brown cow?' ); // false demoMatch ( '/^.ow$/' , 'Zow' ); demoMatch ( '/^.ow/' , 'Zowee' ); demoMatch ( '/w.e$/' , 'Wowee Zowee' ); demoMatch ( '/[WZ]ow/' , 'Wowee Zowee' ); // square-brackets match any one character from the set demoMatch ( '/[WZ]ow/' , 'Yow' ); // false demoMatch ( '/[W-Z]ow/' , 'Yowee' ); // square-brackets can contain a *range* demoMatch ( '/ab[0-9]+de/' , 'ab789de' ); demoMatch ( '/[0-9]*/' , '00047' ); // Beware: matching just a * expression (w/o ^,$)! demoMatch ( '/n[oe]w/' , 'Hew new, brown cow?' ); demoMatch ( '/n[oe]w/' , 'Haw naw, brawn caw?' ); demoMatch ( '/rows?/' , 'How now, browzing cow?' ); demoMatch ( '/rows?/' , 'Hews news, brewns cews?' ); demoMatch ( '/rows?/' , "'Hew new, brewn crew?', he crows." ); /* demoMatch( 'ow', 'How now, brown cow?' ); demoMatch( '/row/', 'How now, brown cow?' ); demoMatch( '/r.w/', 'How now, brown cow?' ); demoMatch( '/n[oe]w/', 'How now, brown cow?' ); demoMatch( '/rows?/', 'How now, brown cow?' ); demoMatch( '/ow/', 'How now, brown cow?' ); demoMatch( '/rows?/', 'How now, brown cow?' ); demoMatch( '/rows?/', 'How now, browsing cow?' ); demoMatch( '/rows?/', 'How now, browssssing cow?' ); */ demoMatch ( '/[0-9]*/' , '00047' ); demoMatch ( '/\\d*/' , '00047' ); demoMatch ( '/[0-9]+/' , '00047' ); demoMatch ( '/^[0-9]*$/' , '00047' ); demoMatch ( '/^[0-9]*$/' , '00047abc' ); demoMatch ( '/^[0-9]*/' , '00047abc' ); demoMatch ( '/^[0-9]*/' , 'abc00047' ); demoMatch ( '/\\d*/' , '00047abc' ); demoMatch ( '/[^0-9]*/' , '00047abc' ); demoMatch ( '/[^0-9]+/' , '00047abc' ); preg_match_all ( '/ow/' , 'How now, brown cow?' , $matches ); print_r ( $matches ); echo "<br />\n" ; echo "parentheses, and backreferences:<br />\n" ; demoMatch ( '/abc/' , 'xyzabcdef' ); demoMatch ( '/(a)b(c)/' , 'xyzabcdef' ); demoMatch ( '/(a)b\\$1/' , 'xyzabcdef' ); demoMatch ( '/(a)b\\$1/' , 'xyzabadef' ); echo "whitespace:<br/>\n" ; demoMatch ( '/\p{Z}/' , "\n\t\r " ); demoMatch ( '/[[:space:]]/' , "\n\t\r " ); echo "calling <code>preg_replace</code>:<br />\n" ; echo preg_replace ( '/abc/' , '!!!' , 'xyzabcdef' ), "<br />\n" ; echo preg_replace ( '/(a)b(c)/' , '!!!' , 'xyzabcdef' ), "<br />\n" ; echo preg_replace ( '/(a)b(c)/' , '(I see $1 and $2)' , 'xyzabcdef' ), "<br />\n" ; echo preg_replace ( '/(a)b(c)/' , '(I see $1 and $2)' , 'xyzabcdef' ), "<br />\n" ; echo preg_replace ( '/(a)b\\$1/' , '(I see $1 and $2)' , 'xyzabadef' ), "<br />\n" ; ?> <hr /> <h3>Source</h3><?php show_source ( __FILE__ ); ?> </body> </html>