Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:42:55+00:00 2026-06-06T19:42:55+00:00

I want to parse a line from a CSV(comma separated) file, something like this:

  • 0

I want to parse a line from a CSV(comma separated) file, something like this:

Bosh,Mark,mark@gmail.com,"3, Institute","83, 1, 2",1,21

I have to parse the file, and instead of the commas between the apostrophes I wanna have ‘;’, like this:

Bosh,Mark,mark@gmail.com,"3; Institute","83; 1; 2",1,21

I use the following Java code but it doesn’t parse it well:

Pattern regex = Pattern.compile("(\"[^\\]]*\")");
        Matcher matcher = regex.matcher(line);
        if (matcher.find()) {
            String replacedMatch = matcher.group();
            String gr1 = matcher.group(1);
            gr1.trim();
            replacedMatch = replacedMatch.replace(",", ";");
            line = line.replace(matcher.group(), replacedMatch);
        }

the output is:

Bosh,Mark,mark@gmail.com,"3; Institute";"83; 1; 2",1,21

anyone have any idea how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:42:57+00:00Added an answer on June 6, 2026 at 7:42 pm

    This is my solution to replace , inside quote to ;. It assumes that if " were to appear in a quoted string, then it is escaped by another ". This property ensures that counting from start to the current character, if the number of quotes " is odd, then that character is inside a quoted string.

    // Test string, with the tricky case """", which resolves to
    // a length 1 string of single quote "
    String line = "Bosh,\"\"\"\",mark@gmail.com,\"3, Institute\",\"83, 1, 2\",1,21";
    
    Pattern pattern = Pattern.compile("\"[^\"]*\"");
    Matcher matcher = pattern.matcher(line);
    
    int start = 0;
    
    StringBuilder output = new StringBuilder();
    
    while (matcher.find()) {
      // System.out.println(m.group() + "\n " + m.start() + " " + m.end());
      output
        .append(line.substring(start, matcher.start())) // Append unrelated contents
        .append(matcher.group().replaceAll(",", ";")); // Append replaced string
    
      start = matcher.end();
    }
    output.append(line.substring(start)); // Append the rest of unrelated contents
    
    // System.out.println(output);
    

    Although I cannot find any case that will fail the method of replace the matched group like you did in line = line.replace(matcher.group(), replacedMatch);, I feel safer to rebuild the string from scratch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag
I want to parse the text line from the Wavefront OBJ file . Currently
I have opened an HTML file using file_get_contents('http://www.example.com/file.html') and want to parse the line
I want to parse a xml file using xquery in Ruby, I found this
fields = CSV.parse(File.open(filename).first)[0] fields.each_with_index do |field, i| fields[i] = field.downcase end I want to
I'm trying to parse each IP line from the following file (loading from the
I am reading a text file from a URL and want to parse the
I want to parse text from a xml file.Consider that I have a some
I am looping over the following lines from a csv file to parse them.
I want to parse out all lines from a multi-line string up until the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.