user1281598
user1281598

Reputation: 317

What is wrong with my pattern?

public class FileParser {

private String filename = "mydata.txt";
private Pattern pattern = Pattern.compile("\\D\\D\\d+");
private Scanner reader;

public FileParser() throws FileNotFoundException{
    reader = new Scanner(new File(filename));
}

public boolean hasMoreData(){
    return reader.hasNext(pattern);
}

public String[] getNextData(){

    return pattern.split(reader.next(pattern));
}

public void close(){
    reader.close();
}
}

So this is my code. I am trying to take out a pattern that consists of 2 letters and a number. Why am I getting nothing/ an empty array from the getNextData() function?

Upvotes: 1

Views: 147

Answers (3)

black panda
black panda

Reputation: 2991

Scanner.hasNext(pattern) searches for the pattern that starts between the delimiter. In your case, the delimiter is a |. But Scanner's default delimiter is whitespace. You have to either manually change your delimiter to be a | or ignore the delimiter altogether. Here's an example using Scanner.findWithinHorizon, which does not care about the delimiter:

public class FileParserExample {
    private Pattern pattern = Pattern.compile("([a-zA-Z]{2})(\\d+)");
    private Scanner reader;


    public FileParserExample(String filename) throws FileNotFoundException {
        reader = new Scanner(new File(filename));
    }

    public boolean hasMoreData() {
        String textFound = reader.findWithinHorizon(pattern, 0);
        return textFound != null;
    }

    public String[] getNextData() {
        MatchResult match = reader.match();
        String [] pieces = {match.group(1), // AB
                            match.group(2) }; // 123
        return pieces;
    }

    public void close() {
        reader.close();
    }

    public static void main(String[] args) throws FileNotFoundException {
        String filename = "mydata.txt";
        FileParserExample parser = new FileParserExample(filename);

        while (parser.hasMoreData()) {
            System.out.println(Arrays.toString(parser.getNextData()));
        }

        parser.close();

    }
}

Upvotes: 1

user590028
user590028

Reputation: 11730

I think your regex is flawed. I believe you want

private Pattern pattern = Pattern.compile("\\w{2}\\d");

Upvotes: 0

Edmund
Edmund

Reputation: 10809

In the expression:

    pattern.split(reader.next(pattern));

reader.next(pattern) is returning the next occurrence of that pattern, e.g. "AB123".

But pattern.split is then splitting that string around the pattern, returning the text on either side of it. Naturally, there is no text on either side, because the separator "AB123" is the whole string.

Do you want to just return the string "AB123" ? You could probably just return the result of reader.next in that case.

Upvotes: 1

Related Questions