Good morning . I have an input and i want to match with the regular expression and get the sentence that matches the output instead of the keywords.
The above code gives the output for the keywords that is matched. I would like to see the sentences instead of keywords.
Is it possible in using Regex ? Still i have seen such things have not been done. So if this method is wrong any other method is possible to get the result?
deepika deepi, I strongly recommend that you learn and follow the Java coding conventions. Doing so will make your code readable, and greatly increase the chances of someone reading it and offering help.
There are no new questions, but there may be new answers.
Totally agree with Darryl.
As for the problem, you can put the text in an array treating one line as one element and then you can loop through that array, find the occurrence of your desired word. If found you can operate on that element.
"Wont write a code for you, that you have to do on your own".
You only need to make a change to your regex. So as not to fall foul of the forum police I'm going to try to describe what is needed without actually giving you the completed regex. Currently your regex only captures the word being searched for so you need to prefix it so as to also match anything up to that word and anything after that word. There are a couple of points to note -
1) If you want to match only isolated words then you will need to make sure the word starts and ends on a word boundary.
2) You should really make sure that any regex meta characters in the word are 'escaped' or you may get unexpected matches.
I surmise from your earlier post that this text you are searching is being read from a file and it seems that you are reading the whole file into memory. This is normally a bad idea since it does not scale well. Your current requirement would be much easier if you did not read the whole file into memory but only read a line at a time.
That sort of post is exactly what will help the original poster the most thank you.
After reading the OP's other threads dealing with his problem I'm not convinced. It seems to me that the OP is in way over his head and probably not going to be helped by what I posted. If I had thought that his basic approach is correct I might have been a little more explicit but reading between the lines I suspect he needs a very different approach so being explicit would not have helped.