• 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

match the starting word

 
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 .happy week end .. I have a pattern matching code to match the particular word from the sentence. In this i am getting result . But the problem is i am getting the result of words that is even in the middle.

code is


input text are reader.txt
Guindy National Park is located in Chennai, South India, one of the last remnants of tropical dry evergreen forest of the Coromandel Coast, Guindy Park was originally a game reserve.

next input of 2.txt

located
biosphere
reserve


The current output is
Number of times Tim is: located
Number of times Tim is: serve

the expected output is
Number of times Tim is: located

i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepika deepi wrote:i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!


That's because your Matcher is not checking for whole words.

Personally, I reckon you're way overthinking this. If I was writing a "word matcher", I wouldn't use regexes at all.

How about this?
1. Read in your text.
2. Break it up into words (see String.split()).
3. Put each of the words into a List - let's call it textWords.
3. Read in your "words to find" and put each word into a List (or Set, depending on how you want to do the match) - let's call that one wordsToFind.
4. For each word in wordsToFind, see if textWords contains that word. If it does, remove it from both Lists.
5. Now check if there are any words left in wordsToFind. If there are, then your match failed. Indeed, your match can fail as soon as it discovers a word that is NOT in textWords.

You could get a lot fancier with it, but right now concentrate on solving the problem.

Winston
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to use an boundary match in this line.



But is does not show any change in the output. for the value given directly it matches.If i use this code it gives the result



How to match for the string instead of key words directly that match with the sentence?

I also will try to do try do you Winston Gutkowski.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepika deepi wrote:How to match for the string instead of key words directly that match with the sentence?


You need to read in your "words to find" and generate a string that looks exactly like your 2nd regex1 string above - and personally, I'd use a StringBuilder, not a String; but it's not essential.

Winston
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

deepika deepi wrote:i want to match only when all the words are there from the 2.txt. if it starts from the middle, It should not match. How to perform that?
Help needed!


That's because your Matcher is not checking for whole words.

Personally, I reckon you're way overthinking this. If I was writing a "word matcher", I wouldn't use regexes at all.

How about this?
1. Read in your text.
2. Break it up into words (see String.split()).
3. Put each of the words into a List - let's call it textWords.
3. Read in your "words to find" and put each word into a List (or Set, depending on how you want to do the match) - let's call that one wordsToFind.
4. For each word in wordsToFind, see if textWords contains that word. If it does, remove it from both Lists.
5. Now check if there are any words left in wordsToFind. If there are, then your match failed. Indeed, your match can fail as soon as it discovers a word that is NOT in textWords.

You could get a lot fancier with it, but right now concentrate on solving the problem.

Winston



I have split the sentence using



i am trying to use that word for matching


But getting an error:
IEinitial.java:438: cannot find symbol
symbol : variable word
location: class IEinitial
Matcher matcher1 = pattern1.matcher(word);


How to use that word for matching and get the result?
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you get error of "can not find symbol" check if you have declared the variable which is causing the trouble, or the variable that you are using should be in the scope.
from you code I can see that the "word" is declared inside the for loop, thats why this will not be available outside for loop.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:Whenever you get error of "can not find symbol" check if you have declared the variable which is causing the trouble, or the variable that you are using should be in the scope.
from you code I can see that the "word" is declared inside the for loop, thats why this will not be available outside for loop.



ya. how to make the word available inside the loop use it outside the loop ?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepika deepi wrote:
I have split the sentence using



i am trying to use that word for matching


But getting an error:
IEinitial.java:438: cannot find symbol
symbol : variable word
location: class IEinitial
Matcher matcher1 = pattern1.matcher(word);


Where you are writing the code for matching the word
I hope not outside your for loop
Consider your code. It is iterating over the String Array and printing each word..
And, if you want to match anything with each of these words,, you need to do that inside that loop only.. Because there's where the variable will be visible..

Deepika, You really need to re-learn the basics of Variable Scope and lifetime.
And I personaly suggest you to write algorithm for whatever problem you have to solve... Go step by step..
And when you are done with the algorithm, go and implement it step by step (One logic at a time)..

Unless the problem domain and logic is not clear in your mind, you may take hours to solve a problem that, you will realise later, just needed few lines of code..

So, think upon it..
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya. i have changed the loop corrected. But i could not see any change in the output even after the split of string into words



can any one tell how to get the result . and why i am not getting ? what i am missing ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic