Pattern Matching to string problme
Ok well the code here works fine for accutally finding the patterns from the file that I am using, the problme is I want to print out the entire string that the pattern is in. it is a text file with this set up
Book Title blah: Author someguy: Num of pages 13: Price 6532.00;
and that is over and over for each boot. Whats printing off is just saying it got a match and where that match is. How can I print off the entire string that it is in?
Thanks for any help
Code Java:
public static void search()
{
try
{
FileInputStream fis = new FileInputStream("Book-text.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
String sStr;
System.out.println("Enter your search");
Scanner sScan = new Scanner(System.in);
sStr = sScan.nextLine();
Pattern pat = Pattern.compile(sStr, Pattern.CASE_INSENSITIVE);
System.out.println("Your searching for " + pat);
System.out.println(dis.available());
StringBuilder sb = new StringBuilder();
//put file into string builder
while(dis.available()!= 0)
{
sb.append(dis.readChar());
}
System.out.println("Before Match " + sb.length());
//Stores the file into one string matcher
String fileText = sb.toString();
// this statement reads the line from the file and print it to
// the console.
Matcher mat = pat.matcher(fileText);
while(mat.find())
{
String result = mat.toString();
System.out.println(result.trim());
}
if(mat.find()==false)System.out.println("No Matcher Found");
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
}catch(IOException e)
{e.printStackTrace();}
catch(PatternSyntaxException e)
{e.printStackTrace();}
}
Re: Pattern Matching to string problme
If you can figure out where the substring is located, why can't you just use the subsring method to extract it?
It might help if you gave us a few more examples of desired input and output.
Re: Pattern Matching to string problme
I used the sub string and that worked fine thanks for that.