0

Hello All,

I am working on a simple program that will counts the number of matches found. I then need to display the text of the last successful pattern and the text the preceeds the last successful pattern match. This is where my problem is. How can I get these matches to display?

Here is my code so far:

#!/usr/local/bin/perl
use English;

$string = "This is my string this is it.  I want to tie my string";

$count = 0;

while ($string =~ m/[string]/gi)
{
  $count=$count+1;
}

print "$count strings\n";
print "\n Match: $MATCH";
print "\n PreMatch: $PREMATCH";

Any assistance will be greatly appreciated.

Thanks,

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by KimJack
0

If you literally want to find "string" you can't put it in square brackets. That makes a character class and order of the characters is ignored.

0
#!/usr/local/bin/perl
use English;

$string = "This is my string this is it.  I want to tie my string";

$count = 0;

while ($string =~ m/string/gi)
{
  $count=$count+1;
}

$string =~ /string/; 
print "$count strings\n";
print "\n Match: $MATCH\n";
print "\n PreMatch: $PREMATCH\n";
print "\n PostMatch: $POSTMATCH";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.