Austin
Austin

Reputation: 51

Use Regex to Find Search Pattern from Text File

I'm writing a program where the user inputs a search pattern which is one word 12 characters or less. The word can be made up of any alpha-numeric combination. I am reading the data from a text file that the user inputs via command line argument. I am able to find the word, but also find unwanted embedded words. For example, If I'm searching "is" and my text file contains "This", it'll tell me it found the word when that is not the desired result.

I placed " " before and after the word, but that eliminates it finding the word if it is the first word in a line. Also, all of characters beside alphanumerics are delimiters. Therefore, if the text file contains "this-dog" and my search pattern is "this", I would want it to return "this" as a match. It should treat the - as a space. This is my code as of right now for this aspect of my program:

try {
            Scanner input = new Scanner(System.in);
            boolean again = true;
            boolean notTheFirst = false;
            while (again) {


                    System.out.printf("%n%s", "Please enter a search pattern: ", "%n");
                    String wordToSearch = input.next();

                    if (wordToSearch.equals("EINPUT")) {
                        System.out.printf("%s", "Bye!");
                        System.exit(0);
                    }

                    String data;
                    int lineCount = 1;

                    try (FileInputStream fis = new FileInputStream(this.inputPath.getPath())) {
                        File file1 = this.inputPath;
                        byte[] buffer2 = new byte[fis.available()];
                        fis.read(buffer2);
                        data = new String(buffer2);
                        Scanner in = new Scanner(data);

                        while (in.hasNextLine()) {

                            String line = in.nextLine();

                            Pattern pattern = Pattern.compile(wordToSearch);
                            Matcher matcher = pattern.matcher(line);

                            if (matcher.find()) {
                                System.out.println("Line number " + lineCount);
                                String stringToFile = f.findWords(line, wordToSearch);
                                System.out.println();
                            }


                            lineCount++;    
                        }


                }
            }

        } catch (IOException e) {
            throw new Exception(e.getMessage());
        }

Upvotes: 0

Views: 2686

Answers (1)

Bohemian
Bohemian

Reputation: 425003

Add "word boundary" terms to each end:

Pattern pattern = Pattern.compile("\\b" + wordToSearch + "\\b");

Upvotes: 1

Related Questions