• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pattern matching problem

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic