List of Special Characters: Note For MS Windows Users
List of Special Characters: Note For MS Windows Users
A regular expression is a pattern that a string is searched for. Unix commands such as "ls *.*" are similar to regular expressions, but the syntax of regular expressions is more elaborated. Several Unix programs (grep, sed, awk, ed, vi, emacs) use regular
expressions and many modern programming languages (such as Java) also support them.
PHP allows for the use of two different types of Regular Expressions: POSIX and Perl-compatible ones. Below are a few example scripts which illustrate how to open a file in PHP, how to use Perl-compatible regular expressions and how to use replacement
and split/implode.
The list of special characters summarises characters that have special meanings or need to be escaped in regular expressions.
<html><head>
<title>Displaying a file line by line</title>
</head><body>
<hr><h1>Results:</h1><hr><p>
<?php
$lines = file('alice.txt');
foreach ($lines as $line_num => $line) {
echo "Line ",$line_num,": ",$line, "<br>\n";
}
?>
<p><hr></body></html>
<html><head>
<title>Searching</title> </head><body>
<hr><h1>Results:</h1><hr><p>
<?php
$lines = file('alice.txt');
foreach ($lines as $line_num => $line) {
if (preg_match("/the /i", $line, $matches)) {
echo "<br> Line ", $line_num, " matches: ",$matches[0],"<br>";
echo $line;
}
}
?>
<p><hr></body></html>
Inserting an "i" after the expression means "ignore case": /the /i.
This manual page provides more details on regular expressions and examples of preg_match.
1.2 Exercises
For the exercises you can use the alice.txt file. (Note for MS Windows users: It is best to save this file by copying and pasting it into a Unix editor. If you save the file under DOS/Windows, each line ends with "\r\n" instead of "\n", which could be a problem.)
1) Retrieve all lines from alice.txt that do not contain /the /. Retrieve all lines that contain "the" with lower or upper case letters.
2) a) Retrieve lines that contain a word of any length that starts with t and ends with e. Modify this so that the word has at least three characters.
b) Retrieve lines that start with a. Retrieve lines that start with a and end with n. Hint: You need to specify the beginning of the line, "a", any number of any characters in the middle, "n", end of line.
c) Retrieve blank lines. Think of at least two ways of doing this.
d) Retrieve lines that contain a word that starts with an upper case letter.
2 Replacement
<html><head>
<title>Searching</title> </head><body>
<hr><h1>Results:</h1><hr><p>
<?php
$lines = file('alice.txt');
foreach ($lines as $line_num => $line) {
$line = preg_replace("/T/", 't', $line);
echo $line, "<br>";
}
?>
<p><hr></body></html>
2.1 Examples
a) ("/[Ll][Oo][Nn][Dd][Oo][Nn]/",'London')
replaces LOndoN or loNDON etc by London.
b) ("/Alice/",'Mary')
replaces every occurrence of Alice by Mary.
2.2 Exercises
By default the multipliers * and + are "greedy" which means they match as many characters as possible. For example, /(\b.+\b)/ matches any non-empty line. A question mark behind a multiplier forces it to be non-greedy. Therefore /(\b.+?\b)/ matches the first
word in a line.
3.1 Exercise
5) Write a replace statement that deletes all HTML markup from a file. You need non-greedy multipliers because otherwise the text between tags may be deleted in a line that contains several tags.
Patterns within parentheses are remembered. Using \1, \2, etc they can be referred to within the same regular expression (or search expression). They can also be echoed using the array provided ("$matches" in the example below).
3.3 Examples
("/(t.*e)/",':\1:',$line)
places a ":" in front and behind each string t...e.
/(...)\\1/
matches a three character string that is repeated.
("/^(.)(.*)(.)$/",'\3\2\1',$line)
switches the first and last character of a line.
3.4 Exercise
6) Print double characters within parenthesis "()". For example, replace "arrived" by "a(rr)ived".
4.1 Exercises
7) Read the alice.txt file into an array. Chomp it. Using "implode" concatenate it into one string. Then split it into words (or sentences) and print it one word (sentence) per line.
8) Write a script that takes an HTML source file as input and prints it so that a newline follows only "closing tags", i.e. tags that are of the form </...>.