正则表达式匹配” | ”

发布于 2024-08-23 22:18:28 字数 419 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我是有多爱你 2024-08-30 22:18:28

我想出了 \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.

知你几分 2024-08-30 22:18:28

下面是一个代码片段,它解析一个字符串(或整个文件,扫描仪接受两者),并从每一行中提取数字和名称:

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();
    }
}

此代码片段产生以下结果:

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

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
电影里的梦 2024-08-30 22:18:28
" \| " 

会起作用,你需要转义引号和 |

" \| " 

would work, you need to escape quotes and the |

兔姬 2024-08-30 22:18:28

不要忘记包含 * 来匹配重复字符

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

已编辑 --
您也可以简单地使用这个 .*\|.*

Dont forget to include the * to match repeating character

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

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

別甾虛僞 2024-08-30 22:18:28

......

^[ \| ]?$

......

^[ \| ]?$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文