Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Pattern Matching to string problme

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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
    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();}
     
       }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Kakashi (February 16th, 2011)

  4. #3
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Pattern Matching to string problme

    I used the sub string and that worked fine thanks for that.

Similar Threads

  1. How to define a string pattern using variables?
    By ice in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2011, 10:45 PM
  2. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  3. to print pattern
    By Charanleen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 3rd, 2010, 07:54 AM
  4. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM