This 0ne Pr0grammer
This 0ne Pr0grammer

Reputation: 2662

How to Specify Multiple Patterns for Delimiter In Java

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!

Upvotes: 0

Views: 1140

Answers (1)

talnicolas
talnicolas

Reputation: 14053

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)

Upvotes: 3

Related Questions