views:

123

answers:

5

Hey guys, I am trying to use Java's useDelimiter method on it's Scanner class to do some simple parsing. Basically each line is a record delimited by " | ", so for example:

2 | John Doe
3 | Jane Doe
4 | Jackie Chan

The method takes as a parameter a regular expression for which to match for. Can someone please provide me with the regular expression that would match | (A vertical bar separated by one space on both sides).

Thanks, I would really appreciate it!

+2  A: 

I came up with \s\|\s which in Java would be expressed as "\\s\\|\\s". I don't know if this is the best one though. I don't need anything hardcore, just something that works, and this seems to :)

Sorry for answering my own question, I guess after typing it out it helped me think.

Jorge Israel Peña
Just be careful, `\s` would be any white-space character, including `\t`! So you may want to be more specific, depending on your input set.
Peter Di Cecco
I agree--I would go with `" \\| "` as my argument to useDelimiter
Michael Brewer-Davis
Ohhh okay, thanks! I didn't know it could be that easy :)
Jorge Israel Peña
A: 
" \| " 

would work, you need to escape quotes and the |

Silmaril89
A: 

Dont forget to include the * to match repeating character

\S*\s*\|\s*[\S\t ]*

Edited -- You can use simply this too .*\|.*

Fadrian Sudaman
Your second regex, `.*\|.*`, would leave spaces on the returned strings...
macek
A: 

......

^[ \| ]?$
Sarfraz
+1  A: 

Here is a code snippet that parses a string (or a whole File, Scanner accepts both), and extracts the number and name from each line :

String s = 
    "1 | Mr John Doe\n" + 
    "2 | Ms Jane Doe\n" + 
    "3 | Jackie Chan\n";

Pattern pattern = Pattern.compile("(\\d+) \\| ((\\w|\\s)+)");
Scanner scan = new Scanner(s);
while (scan.findInLine(pattern) != null) {
    MatchResult match = scan.match();

    // Do whatever appropriate with the results
    System.out.printf("N° %d is %s %n", Integer.valueOf(match.group(1)), match.group(2));

    if (scan.hasNextLine()) {
        scan.nextLine();
    }
}

This code snippet produces the following result :

N° 1 is Mr John Doe
N° 2 is Ms Jane Doe
N° 3 is Jackie Chan
Olivier Croisier