title: Parsing CSV files using Regex in Java
body: <p>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</p>
  
  <pre><code>1997,Ford,E350,"ac, abs, moon",3000.00
  </code></pre>
  
  <p>my output should be </p>
  
  <pre><code>1997 Ford E350 ac, abs, moon 3000.00
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>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 = "\"([^\"]*)\"|(?&lt;=,|^)([^,]*)(?=,|$)";
  
  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();
  
      }
  
  
  
  
  }
  
    }
  </code></pre>
  
  <p>This is my input file </p>
  
  <p>Year,Make,Model,Description,Price</p>
  
  <p>1997,Ford,E350,"ac, abs, moon",3000.00</p>
  
  <p>1999,Chevy,"Venture ""Extended Edition""","",4900.00</p>
  
  <p>1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00</p>
  
  <p>1996,Jeep,Grand Cherokee,"MUST SELL!</p>
  
  <p>air, moon roof, loaded",4799.00</p>
  
  <p>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? </p>