How to Specify Multiple Patterns for Delimiter In Java
so I h开发者_如何学Goave 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!
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)
精彩评论