Exercise Answers: (Ab) A B - / (Ab) .. (Ab)
Exercise Answers: (Ab) A B - / (Ab) .. (Ab)
any string that contains an "a" or "b" followed by any 2 characters followed by an "a" or a
"b". The strings "axxb", "alfa" and "blka" match, and "ab" does not.
Any perl scalar variable name (including the "$"). Perl variable names can contain any
alphanumeric character and the "_" character.
/\$\w+/
Can't do it without something we haven't covered yet! If you try to use something like
/\s+/ it will match any string that contains any whitespace.
/<[aA]\s+[hH][rR][eE][fF]=.*>/
/([a-zA-Z])\1/
Any string that contains an HTML tag and it's corresponding end tag. The following
should match: <H2>Hi Dave</H2> and so should <TITLE>The Test Answers</TITLE>,
but this should not match <TITLE>Not a match</H2>.
/<(\w+)>.*<\/\1>/
The answer above makes some assumptions about what is inside the angle braces (no
whitespace) that are not always true in HTML tags!
Write a perl program that replaces all digits with the name of the digit, so every "0" is
replaced with "zero" , "1" is replaced with "one", ... "9" is replaced with "nine".
Write a perl program that reads in an HTML file (from STDIN) and replaces all
<H1>,</H1> tag pairs with <H3>,</H3> tags.
Here is a better way! A single expression that can replace start or end tags.
while (<>) { # read input one line at a time
s/<(\/?)H1>/<\1H3>/g; # replace all "<H1>" with "<H3>"
# "</H1> with "</H3>"
print;
}
Write a perl program that removes all HTML tags (anything that looks like an HTML tag
- you don't need to check each tag name).
You might need to think about this one! Write a perl program that strips the HEAD
from a HTML file (everything between the <HEAD> tag and the </HEAD> tag. Keep in
mind that in HTML newlines mean nothing - any part of a document can be split amongst
lines any possible way.
HINT: It is much easier to read the entire sequence of lines in to a single perl scalar
variable. Since there are newlines in the single string that contains the entire document -
we need to use the "s" modifier to the substitute command if we want "." to match
newline.
s/(.*?)<HEAD>.*<\/HEAD>(.*)/\1\2/s;
Write a perl program that creates a student record in the form used as input to the above
program. Each line should contain a student name, followed by a tab (no tabs in the name are
allowed), followed by a test1 grade, followed by a tab, etc. A sample output line is:
Joe Smith\t88\t92\t77\n
Your program will accept input in the form of lines that contain name, value pairs with an equal
sign (=) between the name and the value. Here is a sample input file:
name = Joe Student
test1 = 86
test2 = 77
homework = 33
name = Jane Smith
test1 = 98
test2 = 35
homework = 85
Joe Student\t86\t77\t33
Jane Smith\t98\t35\t85
!/usr/bin/perl
@lines = <>;
chomp(@lines);
foreach $i (@lines) {
$i =~ s/[^=]+=\s(.*)/\1/;
}