如何在 Java 中为分隔符指定多种模式
所以我有一种方法可以使用删除“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用正则表达式并创建一个类似于 : 的模式
,然后将该模式提供给
useDelimiter(p)
方法。正则表达式可能类似于
(null\\s)|(null\\(U\\)\\s)
You could use a regex and create a pattern with something like :
and then give that pattern to the
useDelimiter(p)
method.The regex could be something like
(null\\s)|(null\\(U\\)\\s)