in reply to unable chec whether the word is intermediate or last word in a sentence

As I pointed out in Re: need help in building dynamic regular expression, wrap code in <code> tags. Read Writeup Formatting Tips and How (Not) To Ask A Question.

Update: With your legible post, I can now point out a bug. Your path contains the character sequence \d. This is a regular expression metacharacter that means "match any digit". Since you do not intend it in this capacity, you need to escape it before passing it into the regular expression engine. This is usually done with the pair \Q...\E. So your code might look like if( $line =~ /\Q$key\E\\\S+\\/){ # if this is not end node. This is discussed in Quote and Quote like Operators and in Regexp Quote Like Operators in the document perlop.