*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String split on multiple regex" Watch "String split on multiple regex" New topic
Author

String split on multiple regex

Harry Steinke
Greenhorn

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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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
Greenhorn

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