#!/usr/bin/perl use strict; use warnings; sub regex { my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open( my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; while ( <$in> ) { chomp($_); # Solution: 1 substring # if (index($_, $substr) != -1) { # Solution: 2 regex string{times,} at least string 1s # if( $_ =~ /(?:$substr){1,}/) { # Solution: 3 regex match string or more times: + # if( $_ =~ /(?:$substr)+/ ) { # Solution: 4 quotemeta match string if( $_ =~ /\Q$substr\E/ ) { chop($_); # I use chop to remove the last trailing character (}) print $_ . "\n"; push(@_,$_); next; } } # Count the number of signature lines print "I found the string: ".@_." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } regex(); sub my_grep { # Solution 5: grep in perl!!!! (My favorite) my $file = 'test.rtf'; my $substr = 'Date of Last Update:'; open(my $in ,"<", $file) or die "Can not open file: ".$file.": $!\n"; @_ = <$in>; chomp @_; my @out = grep { $_ =~ /Date of Last Update:/ } @_; print "I found the string: ".@out." time(s)!\n"; close ($in) or die "Can not close file: ".$file.": $!\n"; } # &my_grep();