File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String split on multiple regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String split on multiple regex" Watch "String split on multiple regex" New topic

String split on multiple regex

Harry Steinke

Joined: Dec 09, 2005
Posts: 13
I have some text that I wish to parse but there are 2 different characters (, and |) I want to parse it on
ex. some text, with | extra , things in it
Is there a way to do this split once or would I have to do 2 separate splits

String[] split = s.split(",");
String[] split2 = split.split("\\|");
Joel McNary

Joined: Aug 20, 2001
Posts: 1815
See Regualr Expressions. Since the split method takes a regular expression, you can say:

Which means:

Split the string on the ',' or '|' characters. (Note that the vertical pipe is the OR operator in regular expressions.)

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Harry Steinke

Joined: Dec 09, 2005
Posts: 13
That did it.
I agree. Here's the link:
subject: String split on multiple regex
Similar Threads
plz delete
String - Remove dashes from text
Getting user input using Swing
Parsing Strings