Saturday, July 23, 2011

To match a pattern not having a word inbetween perl

To match a pattern not having a word in between

For example if you want to match a pattern "Hi [person's name],how are you" and person should not be karthik like "Hi karthik,how are you"
and the person's name can be anything else.


Content of list.txt:

Hi gopal,how are you
Hi james,how are you
Hi,how are you
Hi Thomas,how are you
Hi Manjula,how are you
Hi karthik,how are you

Content of perl file:

#!/usr/bin/perl
open(FH,")
{
if ($_=~ m/Hi\s+((?!karthik)\w)*\,how are you/ )
{
print;
}
}

Output:

G:\perl_programs>perl perl_avoidword.pl
Hi gopal,how are you
Hi james,how are you
Hi Thomas,how are you
Hi Manjula,how are you

G:\perl_programs>


Analysis:
Here the important part of it is ((?!karthik)\w)*  which specifies karthik should not be there in between but any other word can be there specified by \w


No comments:

Post a Comment