Pattern Matching: Recognizing Numbers
Recognizing Numbers:
#!/usr/bin/perl
@array = (
"There are 812 reasons why I hate you.",
"Give me 7 jellybeans.",
"There are 112 elephants in that line."
);
foreach $line (@array){
if($line =~ /\d+/ ){
print "$&\n";
print "$line\n";
}
}
exit;
PARENTHESES AROUND PORTION OF STRING:
#!/usr/bin/perl
$str = "There are 123 reasons why I hate all 17 of you!";
$str =~ s/(\d+)/($1)/g;
print "$str\n";
exit;
SUBSTITUTING WITH EXPRESSIONS:
#!/usr/bin/perl
$str = "There are 123 reasons why I hate all 17 of you!";
$str =~ s/(\d+)/$1 * 2/eg;
print "$str\n";
exit;
ELIMINATE EVERYTHING EXCEPT NUMBERS:
#!/usr/bin/perl
$n = "";
while(1){
print "INPUT: ";
$n = ;
chomp($n);
if ($n eq "q"){ last; };
$n =~ tr/0-9//cd ;
print "$n\n";
}
exit;