开发者

Searching for exact match is returning substrings.

开发者 https://www.devze.com 2022-12-13 19:47 出处:网络
I have a small java program that searches the contents of all *.txt files in a folder for a specific string.

I have a small java program that searches the contents of all *.txt files in a folder for a specific string.

Example of my problem:

  1. Enter string to find:
  2. Enter string to find: 6570
  3. Enter string to find: 6570

    Found 2 time(s) in kyle.txt!

    Found 2 time(s) in kylezz.txt!

    Press any key to continue . . .


The problem:

It is searching for 6570 however I pickup results for values like this with that string:

11111116570111111
657011111
111116570
6570

Question: I want to search for only an exact string, eg:"6570". how can I make it return only the exact value of 6570? I do not want any extra characters at the beginning or end, only the exact value.

Here is my code:

import java.io.*;
import java.util.*;

public class FileScanner {

    public static void main(String args[]) {

        System.out.print("Enter string to find: ");
        Scanner sc = new Scanner(System.in);
        find(sc.nextLine());

    }

    public static void find(String delim) {
        File dir = new File("files");
        if (dir.exists()) {
            String read;
            try {
                File files[] = dir.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File loaded = files[i];
                    if (loaded.getName().endsWith(".txt")) {
                        BufferedReader in = new BufferedReader(new FileReader(
                                loaded));
                        StringBuffer load = new StringBuffer();
                        while ((read = in.readLine()) != null) {
                            load.append(read + "\n");
                        }
                        String delimiter[] = new String(load).split(delim);
                        if (delimiter.length > 1) {
                            System.out.println("Found "
                                    + (delimiter.length - 1) + " time(s) in "
                                    + loaded.getName() + "!");
                        }
                    }
                }
            } catch (Excep开发者_开发技巧tion e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("error: dir wasn't found!");
        }
    }
}

Thanks guys I really hope you can help me with my programming problem. I've been trying to fix it for about a month now and now I'm asking for help.


Your best bet would be to read Mastering Regular Expressions. In the meantime, perhaps a tutorial on word boundaries would give you the right idea.


A very simple solution would be to add spaces on each side of the string you're searching for.


You do not want to treat your search term as a delimiter. If I have understood your question correctly, you want to match exact words? By "word" I mean space-delimited character strings. Without getting into regular expressions, the simplest thing you could do is:

int matchCount = 0;

while((read = in.readLine()) != null) {

    String[] words = read.split("\\s+");    // split the line on white space

    for (String word : words) {             // loop thru the resulting word array
        if (word.matches(searchTerm)) matchCount++;     // if exact match increment count
    }                                
}

If your search term was "7683" this would match the word "7683" but not "67683" or "7683h" - it is the exact match.


Would something like this work?

  public static void find(String delim) {
    File dir = new File("/tmp/files");
    Pattern strPattern = Pattern.compile(delim);
    if (dir.exists()) {
        try {
            for(File curFile : dir.listFiles()){
              if(!curFile.getName().endsWith(".txt")){continue;}
              BufferedReader in = new BufferedReader(new FileReader(
                       curFile));
              int foundCount = 0;
              String read = null;
              while ((read = in.readLine()) != null) {
                if(strPattern.matcher(read).matches()){
                  foundCount ++;
                }
              }
              System.out.println("Found "+ delim +" "+ foundCount + " time(s) in "+curFile);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("error: dir wasn't found!");
    }
}


Instead of this:

String delimiter[] = new String(load).split(delim);
                                        if(delimiter.length > 1) {
                                                System.out.println("Found " + (delimiter.length - 1) + " time(s) in " + loaded.getName() + "!");
                                        }

you can use this and I think you will get what you want:

String delimiter[] = new String(load).split(" ");
int counter = 0;
for(int i = 0; i < delimeter.length; i++){
    if(delimeter[i].equals(delim)) counter++;
}
System.out.println("Found " + counter + " time(s) in " + loaded.getName() + "!");
0

精彩评论

暂无评论...
验证码 换一张
取 消