in reply to Matching terms in text

Can you tell us how the approach you currently have does not work out?

Maybe show us a small, self-contained working program that shows how things fail for you?

From the look of it, your program should basically work, but maybe there is something I'm missing. Showing a runnable program and telling us how it fails to do what you want would help.