This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes pattern matching problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pattern matching problem" Watch "pattern matching problem" New topic

pattern matching problem

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi all,

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?
Darryl Burke

Joined: May 03, 2008
Posts: 4951

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.

luck, db
There are no new questions, but there may be new answers.
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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".
Richard Tookey

Joined: Aug 27, 2012
Posts: 1166

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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43389
Richard Tookey wrote: . . . forum police . . .
Who are they?

That sort of post is exactly what will help the original poster the most thank you.
Richard Tookey

Joined: Aug 27, 2012
Posts: 1166

Campbell Ritchie wrote:
Richard Tookey wrote: . . . forum police . . .
Who are they?

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.
I agree. Here's the link:
subject: pattern matching problem
It's not a secret anymore!