user1228643
user1228643

Reputation: 75

Regular Expression Statement

I've never been good with regex and I can't seem to get this...

I am trying to match statements along these lines (these are two lines in a text file I'm reading)

Lname Fname 12.35 1
Jones Bananaman 7.1 3

Currently I am using this for a while statement

reader.hasNext("\\w+ \\w+ \\d*\\.\\d{1,2} [0-5]")

But it doesn't enter the while statement. The program reads the text file just fine when I remove the while. The code segment is this:

private void initializeFileData(){
    try {
        Scanner reader = new Scanner(openedPath);

        while(reader.hasNext("\\w+ \\w+ \\d*\\.\\d{1,2} [0-5]")){
            employeeInfo.add(new EmployeeFile(reader.next(), reader.next(), reader.nextDouble(), reader.nextInt(), new employeeRemove()));
        }
        for(EmployeeFile element: employeeInfo){
            output.add(element);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Views: 203

Answers (4)

rlinden
rlinden

Reputation: 2041

I created my own version, without files and the last loop, that goes like that:

private static void initializeFileData() {
        String[] testStrings = {"Lname Fname 12.35 1", "Jones Bananaman 7.1 3"};
        Pattern myPattern = Pattern.compile("(\\w+)\\s+(\\w+)\\s+(\\d*\\.\\d{1,2})\\s+([0-5])");
        for (String s : testStrings) {
            Matcher myMatcher = myPattern.matcher(s);
            if (myMatcher.groupCount() == 4) {
                String lastName = myMatcher.group(1);
                String firstName = myMatcher.group(2);
                double firstValue = Double.parseDouble(myMatcher.group(3) );
                int secondValue = Integer.parseInt(myMatcher.group(4));                
                //employeeInfo.add(new EmployeeFile(lastName, firstName, firstValue, secondValue, new employeeRemove()));
            }
        }
    }

Notice that I removed the slash before the dot (you want a dot, not any character) and inserted the parenthesis, in order to create the groups.

I hope it helps.

Upvotes: 0

Arnab Datta
Arnab Datta

Reputation: 5216

Actually

\w+

is going to catch [Lname, Fname, 12, 35, 1] for Lname Fname 12.35 1. So you can just store reader.nextLine() and then extract all regex matches from there. From there, you can abstract it a bit for instance by :

class EmployeeFile {

 .....

     public EmployeeFile(String firstName, String lastName,
                         Double firstDouble, int firstInt,
                         EmployeeRemove er){
          .....
     }

     public EmployeeFile(String line) {
        //TODO : extract all the required info from the string array
        //       instead of doing it while reading at the same time. 
        //       Keep input parsing separate from input reading.
        //       Turn this into a string array using the regex pattern 
        //       mentioned above

     }



}

Upvotes: 0

FGreg
FGreg

Reputation: 15330

Use the \s character class for the spaces between words:

while(reader.hasNext("\\w+\\s\\w+\\s\\d*\\.\\d{1,2}\\s[0-5]"))

Update:

According to the javadoc for the Scanner class, by default it splits it's tokens using whitespace. You can change the delimiter it uses with the useDelimiter(String pattern) method of Scanner.

private void initializeFileData(){
    try {
        Scanner reader = new Scanner(openedPath).useDelimiter("\\n");
        ...
        while(reader.hasNext("\\w+\\s\\w+\\s\\d*\\.\\d{1,2}\\s[0-5]")){
        ...

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

Upvotes: 6

SwarthyMantooth
SwarthyMantooth

Reputation: 1857

From what I can see (And correct me if I'm wrong, because regex always seems to trick my brain :p), you're not handling the spaces correctly. You need to use \s, not just the standard ' ' character

EDIT: Sorry, \s. Someone else beat me to it :p

Upvotes: 2

Related Questions