Regular Expression to Match " | "

Jorge Israel Peña picture Jorge Israel Peña · Mar 4, 2010 · Viewed 20.4k times · Source

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!

Answer

Jorge Israel Peña picture Jorge Israel Peña · Mar 4, 2010

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.