occurences of words

I have a string like this.

$str="The astrocyte profile might contribute to the identification of possible therapies in profiles profiling and profiled als.";

Lets consider for example:

a)If user enters the term profile* it should highlight profile,profiles only.

b)If user enters the prof* then it should highlight profile,profiles,profiling,profiled.

I have written code like this:


$query="profile*" or "prof*";

$regex=$query;

$regex =~ s/$/.*?/g;

if($str=~/$regex/i)
{
print "\n matched \n";
$str=~s/(\b$regex\b)/<span style="background-color:#E1FF77">$1<\/span>/img;
print "\nmatched sentence = $str \n";
}

I have one problem whatever the query(i.e prof* or profile*) it is highlighting all the words(i.e profile,profiles,profiling,profiled).

How to check that it should highlight given query only i.e (if user enters profile* it should highlight profile,profiles only and if user enters prof* it should highlight profile,profiles,profiling,profiled).

With regards
Vanitha

I think your line

$regex =~ s/$/.*?/g;

just matches the end of the string, whereas what you want to to is to replace the shell glob * with the regexp .*?, i.e.

$regex =~ s/\*/.*?/g;

Otherwise your regexp ends up as profile*.*? which matches "profil" as well.

Hi,

Thanks for the reply!!!!

with regards
Vanitha

You do realize this gives you the first one always, right?

And this gives you a regex string of "profile*.*?" which means "profil" will match.