ssr1012
ssr1012

Reputation: 2589

Matching regex with keyword in a perl script

perl regex is very powerful while using in text content unfortunately I don't get the output what I have expected with my code by doing wrong matches.

In this matches we have n of matches with MBBS and the keyword practitioner.

Sample Input:

Sample text - There are no statistically significant direct effects of prior behavior on practitioner current crop loss, measured by Mr. Srivatsan MBBS M.D. log of amount of crop loss. Third, although we cover a number and given by Mr. Sankaranarayan MBBS possible explanations for what practitioner determines practitioner of crop loss, it is possible that there may be other unobserved, time-varying household characteristics that influence whether a household experience a crop loss. If that is the case the causality from crop loss to fertility decisions may not hold.

My Code:

my $txtfile = "sample.txt";

readFileinString($txtfile, \$string);
my $keyword = "practitioner";

if($string=~m/Mr.\s+(\w+)\s+MBBS(.*?)\s+practitioner\s+/i)
{
    print "$&\n";
}

Code Output:

Mr. Srivatsan MBBS M.D. log of amount of crop loss. Third, although we cover a number and given by Mr. Sankaranarayan MBBS possible explanations for what practitioner

Expected Output:

Mr. Sankaranarayan MBBS possible explanations for what practitioner determines practitioner

Could someone have any experience doing on this way and provide suggestions please.

Upvotes: 0

Views: 50

Answers (1)

ikegami
ikegami

Reputation: 385590

(?:(?!STRING).)* is to STRING as [^CHAR]* is to CHAR.

I think you want

if ($string =~ /(Mr\.\s+(\w+)\s+MBBS(?:(?!MBBS).)*\s+practitioner\s+)/si) {
    print "$1\n";
}

You can add to the inner part of (?:(?!MBBS).)*. For example, you might prefer (?:(?!Mr\.|MBBS).)*.

Upvotes: 2

Related Questions