如何在 Java 中为分隔符指定多种模式

发布于 2024-11-27 16:08:27 字数 1310 浏览 1 评论 0原文

所以我有一种方法可以使用删除“null(U)”引用的分隔符将一个文件复制到另一个文件,并且输入文件看起来像...

C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;null keyword1, keyword2, keyword3, keyword4,

...输出文件看起来像...

C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;null keyword1, keyword2, keyword3, keyword4,

并且对于关于我的分隔符的一小块代码,我有......

Scanner reader = new Scanner(inputFile);
reader.useDelimiter("null\\(U\\) ");

但是,我想知道,如果我想指定分隔符应该查找的多个模式(即除了“null(U)”之外,我想添加“null”),我该怎么做呢?我在网上看到了一些例子,但我仍然不确定分隔符如何区分各种模式。非常感谢任何建议!

so I have a method that copies one file to another with the use of a delimiter that removes the "null(U)" references, and the input file looks like...

C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;null(U) keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;null keyword1, keyword2, keyword3, keyword4,

...and the output file looks like...

C:\Documents and Settings\workspace\Extracted Items\image2.jpeg;image0;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image3.jpeg;image1;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image4.jpeg;image2;keyword1, keyword2, keyword3, keyword4,
C:\Documents and Settings\workspace\Extracted Items\image5.jpeg;image3;null keyword1, keyword2, keyword3, keyword4,

And for the small chunk of code regarding my delimiter, I have...

Scanner reader = new Scanner(inputFile);
reader.useDelimiter("null\\(U\\) ");

However, I was wondering, if I wanted to specify multiple patterns which the delimiter should look for (i.e. in addition to "null (U)", I wanted to add "null"), how would I go about doing that? I've seen a few examples online, but I'm still not sure how the delimiter is able to distinguish between the various patterns. Any advice is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

神魇的王 2024-12-04 16:08:27

您可以使用正则表达式并创建一个类似于 : 的模式

Pattern p = Pattern.compile("your regex here");

,然后将该模式提供给 useDelimiter(p) 方法。

正则表达式可能类似于 (null\\s)|(null\\(U\\)\\s)

You could use a regex and create a pattern with something like :

Pattern p = Pattern.compile("your regex here");

and then give that pattern to the useDelimiter(p) method.

The regex could be something like (null\\s)|(null\\(U\\)\\s)

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