Lesson 5 | String operators |
Objective | Write a program that creates a string and manipulates it using the qq and qw operators. |
Perl String Operators
Just as the math operators work specifically with numbers, there are corresponding string operators for manipulating strings .
Perl String - Exercise
Click the Exercise link belows to write a program in which you will use the string operators we have just examined to manipulate a string and
array.
Perl String - Exercise
Here are some variants, most of which don't work:
$_ = "I have 2 numbers: 53147";
@pats = qw{
(.*)(\d*)
(.*)(\d+)
(.*?)(\d*)
(.*?)(\d+)
(.*)(\d+)$
(.*?)(\d+)$
(.*)\b(\d+)$
(.*\D)(\d+)$
};
for $pat (@pats) {
printf "%-12s ", $pat;
if ( /$pat/ ) {
print "<$1> <$2>\n";
}else {
print "FAIL\n";
}
}
That will print out:
(.*)(\d*) <I have 2 numbers: 53147> <>
(.*)(\d+) <I have 2 numbers: 5314> <7>
(.*?)(\d*) <> <>
(.*?)(\d+) <I have > <2>
(.*)(\d+)$ <I have 2 numbers: 5314> <7>
(.*?)(\d+)$ <I have 2 numbers:> <53147>
(.*)\b(\d+)$ <I have 2 numbers:> <53147>
(.*\D)(\d+)$ <I have 2 numbers:> <53147>