Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String split on multiple regex

 
Harry Steinke
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Harry Steinke
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did it.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic