how to match the key word from a text file

Ads
 

how to match the key word from a text file

p>Hi all, I have the code to match the key word and from the text. I have input like this 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.

and 2.txt is located biosphere reserve

I am getting an output like this now. Number of times Tim is: located Number of times Tim is: serve

the expected output is Number of times Tim is: located

code is as follows
**import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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"));
String devstr;
while ((devstr = br.readLine()) != null) {
String text = devstr;
String text1 = text.replaceAll("  ","").replaceAll("   "," ");
    System.out.println(text1);  
String[] words = text1.split(" ");
for (String word : words)  
 {  
String regex1 = "\\b"+data2+"\\b";
Pattern pattern1 = Pattern.compile(regex1);
Matcher matcher1 = pattern1.matcher(word);
String val = null;
  while (matcher1.find()) {
val = matcher1.group();
System.out.println("Number of times Tim is: " +val );
 }
}
}
} catch (IOException e) {
}//try catch ends
}
}**

If i give the input like this String regex1 = "\blocated|reserve|biosphere\b";

instead of this i am getting the result. I don't want to hard code the string. instead i want to get the value from the called file and get the result. String regex1 = "\b"+data2+"\b";

i am missing some part to get the correct output and not able to find that. If any one could find my mistake help me. please...

View Answers

Ads









Related Tutorials/Questions & Answers:
how to match the key word from a text file
how to match the key word from a text file  p>Hi all, I have the code to match the key word and from the text. I have input like this reader.txt... want to get the value from the called file and get the result. String regex1
Java search word from text file
Java search word from text file In this tutorial, you will learn how to search a word from text file and display data related to that word. Here, we have created a text file student.txt which consists of id, name and marks of few
Advertisements
stop word removal from text file
stop word removal from text file  i need java source code for stop word removal from a text file
Text field save as word file
Text field save as word file  Dear experts At run how to save set of text field contains text into single Word file. To save in our desktop computer. Solve my problem
click a word and search it from a file
click a word and search it from a file  i want to link a word with a file and search it form the file by clicking it. what could the code for this process
Count repetitions of every word from an input file
Count repetitions of every word from an input file  Hello..i got to know how can i count the repetitions of every word present in a specific input file?For suppose if i have a url log file which have time,url's,ip address etc
How to remove paragraph from ms word
How to remove paragraph from ms word  Hi! I am using MS Word to edit my text but somehow i get paragraph sign on every line of my text. So, can any one please tell me how to remove paragraph from ms word?   CTRL+SHIFT+8
Read Lines from text file
read from the text file and displays the output as desired. Unable to read the rest...Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
how to convert image file to text file?
how to convert image file to text file?  hi, can anybody tell how to convert image file to text file? plz help me
Create text file at client's directory from server.
Create text file at client's directory from server.  Need java code to create text file at client's directory from server..... Please Help
binary search tree from text file
binary search tree from text file  How so I go about constructing a binary search tree from a text file, which has letters and numbers, which must be sorted and printed in ascending order. E.g. Text file contents 3 apples pears
How to hide text in Word Document using Apache POI library
How to hide text in Word Document using Apache POI library  Is it possible to hide text in MS Word Document using Apache POI library? If yes, please let me know how. I am able to create and read the document using POI library
Count characters from text file in Java
Count characters from text file in Java  At the "Count chracters fro... is a java code that count the occurrence of each character from text file. import... the directory of the text file ("C:\text.txt"). I use Eclipse to run this code. I
How to append text to an existing file in Java
How to append text to an existing file in Java  How to append text to an existing file in Java
How to copy text from a gif image
How to copy text from a gif image  I have some gif images that containing some important text. i want to have that text in my notepad file. i have.... so, please tell me how can i copy that text to my notepad. i am using ubuntu
how to read a text file with scanner in java
how to read a text file with scanner in java  Hi, I am looking for the example code in Java for reading text file line by line using the Scanner class. how to read a text file with scanner in java? Thanks   Hi
how to write file from FileInputStream
how to write file from FileInputStream  Hi, How to Write a file.... Thanks,   Hi, For Write to file from FileInputStream in java you may use... bytes from a file or can say it reads the streams of bytes. For Reference visit
need to update key , value pair in map from properties file
need to update key , value pair in map from properties file  I have a map with key , value pair. with key as integer and value as string datatype. I... .properties file where i will update the keys and values whenever needed. i want
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
How to write file text In New Line
How to write file text In New Line  Hi friends, How to write file text in new line in Java program. How to write the syntax for this with example... you went new text in new line. For this we created a new text file named
how to fetch values from .properties to a html file
how to fetch values from .properties to a html file  I have a .properties file with some key value pairs in it. I need to fetch the values from this .properties file into a html file.Please let me know how to do
How to Convert Text Files into Gzip File
How to Convert Text Files into Gzip File  Hi, I am developing a small application using PHP language. The real problem is that how do i convert the text files into gzip file. how can i solve the problem.... Tnanks
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3 columns contigID length size and then display sequence like "ATGCGSA..." in text
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
how to sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file is: contig00001 length=586 numreads=4 CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
Compare two word file
Compare two word file  How to compare two word file using Java

Ads