正则表达式匹配” | ”
我正在尝试使用Java的 useDelimiter
方法在它的 Scanner
类上进行一些简单的解析。基本上每一行都是由“ | ”分隔的记录,因此例如:
2 | John Doe
3 | Jane Doe
4 | Jackie Chan
该方法将要匹配的正则表达式作为参数。有人可以给我提供匹配 |
的正则表达式(两边用一个空格分隔的竖线)。
谢谢,我真的很感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想出了
\s\|\s
,在 Java 中将表示为"\\s\\|\\s"
。我不知道这是否是最好的。我不需要任何硬核的东西,只需要一些有用的东西,这似乎:)很抱歉回答我自己的问题,我想在输入它之后它帮助我思考。
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.
下面是一个代码片段,它解析一个字符串(或整个文件,扫描仪接受两者),并从每一行中提取数字和名称:
此代码片段产生以下结果:
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 :
This code snippet produces the following result :
会起作用,你需要转义引号和 |
would work, you need to escape quotes and the |
不要忘记包含 * 来匹配重复字符
已编辑 --
您也可以简单地使用这个
.*\|.*
Dont forget to include the * to match repeating character
Edited --
You can use simply this too
.*\|.*
......
......