Or (this horse ain't quite dead, yet),
#!/usr/bin/perl
use strict;
use warnings;
my(@lines, $line);
print "\n\tUsing tr\n";
@lines = <DATA>;
for $line(@lines) {
chomp $line;
my $line1 = $line;
if ( ($line1 =~ tr/[r|R]/r/) == 4 ) { # Case insensitive
print "$line1\n";
}
}
print "\n\t using match:\n";
for $line(@lines) {
if ( $line =~ /
^ # start at beginning of line
([^rR]*) # 0 or more non-r chars
r # match "r"
([^rR]*) # negative lookahead, 0 or more non-r chars
r
([^rR]*) # alternately, could be ?=[^r]*
r
([^rR]*)
r
([^rR]*)
$ # end of line
/ix) { # extended syntax; end of match; end of conditio
+n
print "$line\n"
}
}
print "\n\t using match2:\n";
for $line(@lines) {
if ( $line =~ /^([^r]*|[^R]*)r([^r]*|[^R]*)r([^r]*|[^R]*)r([^r]*|[
+^R]*)r([^r]*|[^R]*)$/i) {
print "$line\n";
}
}
print "\n and the data is:\n";
for $line(@lines) {
print $line . "\n";
}
print "\nDone\n";
__DATA__
There are four "r"s in this sentence. 4
This one has how many? 0
None in this. 0
But where can there be as many words with 'r's as there are here? 7
Still, rrrr makes no sense. 4
Drill for sentences with multiple instances of "are" and "were" regula
+rly. 5
There are four "r"s in this sentence. 4
This one has how many? 0
None in this. 0
But where can there be as many words with 'r's as there are here? 7
Still, rrrr makes no sense. 4
Drill for sentences with multiple instances of "are" and "were" regula
+rly. 5
Argh. Right you are, Randy! 4 matches if insensitive (but only 2 match
+ when case sensitive).
output
Using tr
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
Argh. right you are, randy! 4 matches if insensitive but only 2 match
+when case sensitive.
using match:
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
Argh. Right you are, Randy! 4 matches if insensitive but only 2 match
+when case sensitive.
using match2:
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
There are four "r"s in this sentence. 4
Still, rrrr makes no sense. 4
Argh. Right you are, Randy! 4 matches if insensitive but only 2 match
+when case sensitive.
and the data is: ....
Re-updated; Found copied wrong right code. duh!
And, of course, this being Perl, there are many other ways, too. One could (if one wished to besmirch the virtue of laziness, capture each [rR], push them to an array, and count the elements. But bottom line: tr/r/r/ (preserving the original data, case sensitive) or tr/[r|R]/r/ (counting the upper case "R"s, but losing the original case) may be preferred.
|