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,

Recommended Answers

All 3 Replies

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.

#!/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";

Thank you for all of the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.