This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

String.split() question

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a String that is delimited by commas

Value1, value2 , value3

However i want to be able to allow a value that has a comma. So it would have to be escaped.....perahps

value1, value2, value3, value4\,HasAComma


Is there a way to use String.split() to parse this such that the returned values
are

value1
value2
value3
value4,HasAComma

Thanks for any advice. wasn't sure if this could be accomplished via a regular expression or not.
 
author & internet detective
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephanie Smith wrote:Thanks for any advice. wasn't sure if this could be accomplished via a regular expression or not.


Yes. In the Pattern class JavaDoc look at positive/negative lookbehind. That lets you check the character before the character you are searching for.
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But think about this: what if you want to end such a value with \? Do you then escape that too, e.g. value1, value2, value3, value4 ends with slash\\,value5? Because then it's going to be a bit harder; you have to check the number of preceding backslashes - even means don't escape the comma, odd means do.

But in the end you'll be redefining the CSV format. It has already thought of these issues. It uses value quoting to allow commas inside values, but also allows quotes inside values. Check out AccessingFileFormats under the Excel section to find a few libraries that can read and write to CSV files.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic