RU beehive logo ITEC dept promo banner
ITEC 325
2011fall
ibarland

homelectsexamshws
breeze (snow day)

lect04b-regexps
input validation
functions; miscellania

PHP regular expressions

PHP regular-expression matching: Regular expressions a strings delimited by a special character (usually /). Some very quick examples: regexp-match-quick-example.php
      echo preg_match( '/row/', 'How now, brown cow?' );
      echo preg_match( '/.ow/', 'How now, brown cow?' );
      echo preg_match( '/n[oe]w/', 'How now, brown cow?' );
      echo preg_match( '/rows?/', 'How now, brown cow, eh?' );
      echo preg_match( '/rows?/', 'How now, brown cows, eh?' );
      echo preg_match( '/rows?/', 'How now, brown cowss, eh?' );

      echo preg_match_all( '/ow/', 'How now, brown cow?' );
      echo preg_match_all( '/rows?/', '\"How now, brown cow?\", he crows.' );

      echo preg_match( '/[0-9]*/', '00047' );
      echo preg_match( '/\\d*/',   '00047' );
      echo preg_match( '/[0-9]+/', '00047' );
    
See the manual.

Note: the name “preg” comes from "Perl compatible"; earlier PHP used the POSIX regexp's but PHP decided to deprecate that.

Atomic regexps

Compound regexps

There are also ways of building bigger regexps out of smaller ones:

NOTE: No trailing “g” allowed, as in javascript!
(There are other trailing modifiers however — e.g. i for case-insensitive, and more.

Three helpful functions:

homelectsexamshws
breeze (snow day)


©2011, Ian Barland, Radford University
Last modified 2011.Sep.23 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme