Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split string with rules  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need your regular expression skill to help with finetuning this Java String.split(",") that is not retaining all the delimiter correctly. Below is the type of input string used:





i am looking for a clean as simple solution instead of with StringTokenizer or LinkedList. Would finetuning the regular expression achieve the objective? Otherwise, please advice on other possible better solution.

Thanks in advance,
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd be better off using a 3rd party CSV library rather than trying to write your own regex.
See AccessingFileFormats for a few suggestions (under the Excel section)
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely agree with Adrian on this one.
The apparent simplicity of the CSV format can be deceptive, and trying to parse it using your own regex solution is often not the way to go.
Save yourself some time and frustration and go with a ready-made CSV parser (e.g. opencsv, apache commons CSV, CsvJdbc)
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks folks. OpenCSV works like a charm.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!