34

I would like to parse entire file based on all the possible delimiters like commas, colon, semi colons, periods, spaces, hiphens etcs.

Suppose I have a hypothetical string line "Hi,X How-how are:any you?" I should get output array with items Hi,X,How,how,are,any and you.

How do I specify all these delimiter in String.split method?

Thanks in advance.

0

1 Answer 1

37

String.split takes a regular expression, in this case, you want non-word characters (regex \W) to be the split, so it's simply:

String input = "Hi,X How-how are:any you?";
String[] parts = input.split("[\\W]");

If you wanted to be more explicit, you could use the exact characters in the expression:

String[] parts = input.split("[,\\s\\-:\\?]");
Sign up to request clarification or add additional context in comments.

7 Comments

why the or-ing operator in that expression above? Are they necessary?
@Hovercraft - no, but for me it's easier to read, so that's what I go with.
My own preference is to show a newbie the regex without the unnecessary clutter. YMMV.
@UmeshKacha: please have a look at the tutorial section on this: predefined character classes. Shoot the whole tutorial is worthwhile. Then when done with this one, graduate to this one which is my favorite.
@MarkElliot I'm fairly sure that character class will match the pipe (|) character, rather than treating them as "or" instructions. This isn't really a problem in this case, but you should be aware that it's not doing what you think it's doing. It could cause confusion and bugs in more complicated code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.