$line =~/the /
searches for the occurrence of the four character sequence "the " in the
string in $line. If "!~" was used instead of "=~" then the script
would search for strings that do not contain the four character
sequence "the ".
# searching the file content line by line:
foreach $line (@lines){
if ($line =~/the /){
print $line;
} # end of if
} # end of foreach
1) Retrieve all lines from alice.txt that do not contain /the /. Retrieve all lines that contain "the" with lower or upper case letters (hint: inserting an "i" after the expression means "ignore case":/the /i).
2)
a) Retrieve lines that contain a three letter string consisting of
"t", then any character,
then "e", such as "the dog", "tree", "not ever".
b) Retrieve lines with a three letter word that starts with t and
ends with e.
c) 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.
d) Retrieve lines that start with a. Retrieve lines that start with a and
end with n.
e) Retrieve blank lines. Think of at least two ways of doing this.
f) Retrieve lines that have two consecutive o's.
g) Retrieve lines that do not contain the blank space character.
h) Retrieve lines that contain more than one blank space character.
3) For the following regular expressions write a script that lets a user input a string. The string is then compared to a regular expression (using =~). A message is printed to the screen if the match was successful. (Alternatively you could keep using the alice.txt file and simply add a couple of lines to it that contain these patterns.)
Match the following patterns:
a) an odd digit followed by an even digit (eg. 12 or 74)
b) a letter followed by a non-letter followed by a number
c) a word that starts with an upper case letter
d) the word "yes" in any combination of upper and lower cases letters
e) one or more times the word "the"
f) a date in the form of one or two digits, a dot, one or two digits,
a dot, two digits
g) a punctuation mark
4) What is the difference between the following expressions?
a) abc* and (abc)*
b) !/yes/ and /[^y][^e][^s]/
c) [A-Z][a-z]* and [A-Z][a-z]+
5) Write a script that asks users for their name, address and phone number. Test each input for accuracy, for example, there should be no letters in a phone number. A phone number should have a certain length. An address should have a certain format, etc. Ask the user to repeat the input in case your script identifies it as incorrect.
#!/usr/local/bin/perl
#
# Regular expressions
#
open(ALICE, "alice.txt");
while (<ALICE>){
if (/the /){
print $_;
} # end of if
} # end of while
close(ALICE);
# searching the file content line by line:
foreach $line (@lines){
$line =~s/T/t/g;
print $line;
} # end of foreach
s/(t.*e)/:$1:/g;
places a ":" in front and behind each string t...e.
/(...)\1/
matches a three character string that is repeated.
s/^(.)(.*)(.)$/$3$2$1/
switches the first and last character of a line.
$` | contains the string before the pattern |
$& | contains the pattern that is matched |
$' | contains the string after the pattern |
For example:
$line="The cat that sat on the mat.";
$line =~ /c.t/;
$` contains "The "
$& contains "cat"
$' contains " that sat on the mat."
11) 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 </...>.
12) Optional: Parsing web pages
If a CGI script downloads a page from the web, it will retrieve the
HTML source code. Look at several html pages on the web.
Think about the following questions: How would you
extract information from them? How would you store that information in
an array? How would a script search for words
(or regular expressions) in web pages? Besides the fact that you don't
know yet how a CGI script can download pages from the web, have you
learned enough so far that you could write such a script?
. | Any single character except a newline |
^ | The beginning of the line or string |
$ | The end of the line or string (Use "\r$" instead of "$" for end of line in DOS/Windows.) |
* | Zero or more of the last character |
+ | One or more of the last character |
? | Zero or one of the last character |
{5,10} | Five to ten times the previous character |
for example: * equals {0, }; + equals {1, } | |
? equals {0,1} |
[qjk] | Either q or j or k |
[^qjk] | Neither q nor j nor k |
[a-z] | Anything from a to z inclusive |
[^a-z] | No lower case letters |
[a-zA-Z] | Any letter |
[a-z]+ | Any non-zero sequence of lower case letters |
jelly|cream | Either jelly or cream |
(eg|le)gs | Either eggs or legs |
(da)+ | Either da or dada or dadada or... |
\n | A newline |
\t | A tab |
\w | Any alphanumeric (word) character. |
The same as [a-zA-Z0-9_] | |
\W | Any non-word character. |
The same as [^a-zA-Z0-9_] | |
\d | Any digit. The same as [0-9] |
\D | Any non-digit. The same as [^0-9] |
\s | Any whitespace character: space, |
tab, newline, etc | |
\S | Any non-whitespace character |
\b | A word boundary, outside [] only |
\B | No word boundary |
\| | Vertical bar |
\[ | An open square bracket |
\) | A closing parenthesis |
\* | An asterisk |
\^ | A carat symbol |
\/ | A slash |
\\ | A backslash |