CSE 134A
Discussion section
Friday, 11/9/2002
TA: Greg Hamerly
Hi!
Today we will discuss problems 3 and 4 from Wednesday's midterm, and using PHP
regular expressions to extract information from strings.
Project 2 will be graded and handed back on Monday during office hour. Monday is
a holiday, so there will only be one office hour (4-5PM).
------------------------------------------------------------------------
Exercises in regular expressions: work on these in groups.
(1) What do the following regular expressions extract? What will be the value of
the variable $match after each expression executes, assuming that $str has the
patterns we are looking for?
(a) ereg("UCSD is a very ([^ ]+) school", $str, $match);
(b) eregi("
([^<]+)", $str, $match);
(c) ereg("(A[[:digit:]]{8})", $str, $match);
(d) ereg("function +([a-zA-Z_][[:alnum:]]*) *\(", $str, $match);
(2) Write regular expressions using PHP functions to *extract* the following
items from a string $str:
(a) A Social Security number using this format: 123-45-6789
(b) The target and textual description of a URL. For example, extract
"http://www.google.com/" and "Google" from this string:
"Please click here: Google..."
(c) The last HTML tag in a document (where you may assume the entire
document is in the variable $str).
(d) A number between 1000 and 2000 (inclusive).
Notes:
- Remember these special operators when using regular expressions:
[], ^, $, ., |, *, +, (), {}, etc.
- There are many useful functions in PHP for regular expressions, including
ereg(), eregi(), ereg_replace(), split(), join(), and Perl-style regular
expression functions like preg_match(), preg_match_all(), preg_replace(),
etc.