Deepika Deepi 0 Newbie Poster

hi all,

Good noon. I am trying to get the match a key word from text, get the input from text and if the match in the text matches with the input. I need to get the result of that. If the word starts in the middle i should not get the result of the word in the input.

class Classnew
{
public static String getnew()
{
String data2 = ""; 
try {
BufferedReader br = new BufferedReader(
new FileReader("2.txt"));
String devstr;
while ((devstr = br.readLine()) != null) {
System.out.println(devstr);
data2+=devstr+"\n";
}
} catch (IOException e) {
}
return data2;
}
}

public class Findwordphrase {
public static void main(String[] args) {
Classnew classnew = new Classnew(); 
     String data2 =classnew.getnew();
    System.out.println("Data of Class2(keyWrd): " + data2);
try {
BufferedReader br = new BufferedReader(new FileReader("reader.txt"));
//FileInputStream fstream = new FileInputStream("1.txt");
//DataInputStream in = new DataInputStream(fstream);
//BufferedReader br = new BufferedReader(new InputStreamReader(in));

String devstr;
while ((devstr = br.readLine()) != null) {
String output = devstr;
//System.out.println(output);
String regex1 = data2;
Pattern pattern1 = Pattern.compile(regex1);
  Matcher matcher1 = pattern1.matcher(output);
String val = null;
  while (matcher1.find()) {
//  count++;
val = matcher1.group();
System.out.println("Number of times Tim is: " +val );
//  System.out.println("Number of times Tim is: " + count);
//  System.out.println("Tim Start at the index: " + matcher.start());
 }
}

} catch (IOException e) {
}//try catch ends

}
}

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 am trying to use an boundary match in this line.

String regex1 = "\\b"+ data2+"\\b";

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

String regex1 = "\\blocated|reserve|biosphere\\b";

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

help needed. thank's for reading this post... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.