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 8539935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:29:05+00:00 2026-06-11T11:29:05+00:00

I’m trying to create a program, which reads CSV files from a directory, using

  • 0

I’m trying to create a program, which reads CSV files from a directory, using a regex it parses each line of the file and displays the lines after matching the regex pattern.
For instance if this is the first line of my csv file

1997,Ford,E350,"ac, abs, moon",3000.00

my output should be

1997 Ford E350 ac, abs, moon 3000.00

I don’t want to use any existing CSV libraries. I’m not good at regex, I’ve used a regex I found on net but its not working in my program
This is my source code, I’ll be grateful if any one tells me where and what I”ve to modify in order to make my code work. Pls explain me.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class RegexParser {

private static Charset charset = Charset.forName("UTF-8");
private static CharsetDecoder decoder = charset.newDecoder();
String pattern = "\"([^\"]*)\"|(?<=,|^)([^,]*)(?=,|$)";

void regexparser( CharBuffer cb)
{ 
    Pattern linePattern = Pattern.compile(".*\r?\n");
    Pattern csvpat = Pattern.compile(pattern);
    Matcher lm = linePattern.matcher(cb);
    Matcher pm = null;

    while(lm.find())
    {   
        CharSequence cs = lm.group();
        if (pm==null)
            pm = csvpat.matcher(cs);
            else
                pm.reset(cs);
        if(pm.find())
                     {

            System.out.println( cs);
                      }
        if (lm.end() == cb.limit())
        break;

        }

    }

public static void main(String[] args) throws IOException {
    RegexParser rp = new RegexParser();
    String folder = "Desktop/sample";
    File dir = new File(folder);
    File[] files = dir.listFiles();
    for( File entry: files)
    {
        FileInputStream fin = new FileInputStream(entry);
        FileChannel channel = fin.getChannel();
        int cs = (int) channel.size();
        MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, cs);
        CharBuffer cb = decoder.decode(mbb);
        rp.regexparser(cb);
        fin.close();

    }




}

  }

This is my input file

Year,Make,Model,Description,Price

1997,Ford,E350,”ac, abs, moon”,3000.00

1999,Chevy,”Venture “”Extended Edition”””,””,4900.00

1999,Chevy,”Venture “”Extended Edition, Very Large”””,””,5000.00

1996,Jeep,Grand Cherokee,”MUST SELL!

air, moon roof, loaded”,4799.00

I’m getting the same as output where is the problem in my code? why doesn’t my regex have any impact on the code?

  • 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-11T11:29:06+00:00Added an answer on June 11, 2026 at 11:29 am

    Anyway I’ve found the fix myself, thanks guys for your suggestion and help.

    This was my initial code

        if(pm.find()
            System.out.println( cs);
    

    Now changed this to

      while(pm.find()
      {
     CharSequence css = pm.group();
     //print css
       }
    

    Also I used a different Regex. I’m getting the desired output now.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.