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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:20:56+00:00 2026-06-18T01:20:56+00:00

I have this kind of CSV file which I have to parse in Java.

  • 0

I have this kind of CSV file which I have to parse in Java.

2012-11-01 00,  1106,   2194.1971066908
2012-11-01 01,  760,    1271.8460526316
.
.
.
2012-11-30 21,  1353,   1464.0014781966
2012-11-30 22,  1810,   1338.8331491713
2012-11-30 23,  1537,   1222.7826935589
        
720 rows selected.      
        
Elapsed: 00:37:00.23

This is Java code I created in order to segregate each column and store it in a list.

public void extractFile(String fileName){
        try{
            BufferedReader bf = new BufferedReader(new FileReader(fileName));
            try {
                String readBuff = bf.readLine();
                
                while (readBuff!=null){
                    
                    Pattern checkData = Pattern.compile("[a-zA-Z]");
                    Matcher match = checkData.matcher(readBuff);
                    
                    if (match.find()){
                        readBuff = null;
                    }
                    
                    else if (!match.find()){
                        
                        String[] splitReadBuffByComma = new String[3];
                        splitReadBuffByComma = readBuff.split(",");
                        
                            for (int x=0; x<splitReadBuffByComma.length; x++){
                                
                                if (x==0){
                                    dHourList.add(splitReadBuffByComma[x]);
                                }
                                else if (x==1){
                                    throughputList.add(splitReadBuffByComma[x]);
                                }
                                else if (x==2){
                                    avgRespTimeList.add(splitReadBuffByComma[x]);
                                }
                            }
                    }
                    
                    readBuff = bf.readLine();
                }
            }
            finally{
                bf.close();
            }
        }
        catch(FileNotFoundException e){
            System.out.println("File not found dude: "+ e);
        }
        catch(IOException e){
            System.out.println("Error Exception dude: "+e);
        }
    }

The problem is that the regex I created is a bit faulty because it still includes the text "720 rows selected" and stores them in dHourList.
dHourList should only store the date column represented like this "2012-11-01 00…etc"
throughputList = "1106, 760 …etc"
avgResponseTime = "2194.192, 1271.846…etc"

What should be the correct reg expression for this?

Update

2012-11-30 21
2012-11-30 22
2012-11-30 23

720 rows selected.

Elapsed: 00:37:00.23

Size of date-hour: 724 size of throughput: 720 size of avg resp time: 720

I used this in checkData regex instead because if I use one slash \d compile will say invalid escape sequence

Pattern checkData = Pattern.compile("^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])\b.+$"); 

but its still showing 720 rows selected and another line which shouldn’t be there.

Update 2

Working code:

while (readBuff!=null){
                    
                    
                    Pattern checkData = Pattern.compile("^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])\\b.+$");
                    
                    Matcher match = checkData.matcher(readBuff);
                    
                    if (!match.find()){
                        readBuff = null;
                    }
                    
                    else{
                        
                        String[] splitReadBuffByComma = new String[3];
                        splitReadBuffByComma = readBuff.split(",");
                        
                            for (int x=0; x<splitReadBuffByComma.length; x++){
                                
                                if (x==0){
                                    dHourList.add(splitReadBuffByComma[x]);
                                }
                                else if (x==1){
                                    throughputList.add(splitReadBuffByComma[x]);
                                }
                                else if (x==2){
                                    avgRespTimeList.add(splitReadBuffByComma[x]);
                                }
                            }
                    }
                    
                    readBuff = bf.readLine();
                }

I removed else if condition and changed it to else and used the regex suggested by Cylian
now I have the output

2012-11-30 21
2012-11-30 22
2012-11-30 23

Size of date-hour: 720 size of throughput: 720 size of avg resp time: 720

Thanks alot!

  • 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-18T01:20:57+00:00Added an answer on June 18, 2026 at 1:20 am

    Try this [your code, but a bit modified]:

    public void extractFile(String fileName){
            try{
                BufferedReader bf = new BufferedReader(new FileReader(fileName));
                try {
                    String readBuff = bf.readLine();
    
                    while (readBuff!=null){
    
                        Pattern checkData = Pattern.compile("^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])\\b.+$");
                        Matcher match = checkData.matcher(readBuff);
    
                        if (!match.find()){
                            readBuff = null;
                        }
    
                        else if (match.find()){
    
                            String[] splitReadBuffByComma = new String[3];
                            splitReadBuffByComma = readBuff.split(",");
    
                                for (int x=0; x<splitReadBuffByComma.length; x++){
    
                                    if (x==0){
                                        dHourList.add(splitReadBuffByComma[x]);
                                    }
                                    else if (x==1){
                                        throughputList.add(splitReadBuffByComma[x]);
                                    }
                                    else if (x==2){
                                        avgRespTimeList.add(splitReadBuffByComma[x]);
                                    }
                                }
                        }
    
                        readBuff = bf.readLine();
                    }
                }
                finally{
                    bf.close();
                }
            }
            catch(FileNotFoundException e){
                System.out.println("File not found dude: "+ e);
            }
            catch(IOException e){
                System.out.println("Error Exception dude: "+e);
            }
        }
    

    regex anatomy

    # ^(19|20)\d\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])\b.+$
    # 
    # Options: ^ and $ match at line breaks
    # 
    # Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
    # Match the regular expression below and capture its match into backreference number 1 «(19|20)»
    #    Match either the regular expression below (attempting the next alternative only if this one fails) «19»
    #       Match the characters “19” literally «19»
    #    Or match regular expression number 2 below (the entire group fails if this one fails to match) «20»
    #       Match the characters “20” literally «20»
    # Match a single digit 0..9 «\d»
    # Match a single digit 0..9 «\d»
    # Match the regular expression below and capture its match into backreference number 2 «([-/.])»
    #    Match a single character present in the list “-/.” «[-/.]»
    # Match the regular expression below and capture its match into backreference number 3 «(0[1-9]|1[012])»
    #    Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
    #       Match the character “0” literally «0»
    #       Match a single character in the range between “1” and “9” «[1-9]»
    #    Or match regular expression number 2 below (the entire group fails if this one fails to match) «1[012]»
    #       Match the character “1” literally «1»
    #       Match a single character present in the list “012” «[012]»
    # Match the same text as most recently matched by capturing group number 2 «\2»
    # Match the regular expression below and capture its match into backreference number 4 «(0[1-9]|[12][0-9]|3[01])»
    #    Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
    #       Match the character “0” literally «0»
    #       Match a single character in the range between “1” and “9” «[1-9]»
    #    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[12][0-9]»
    #       Match a single character present in the list “12” «[12]»
    #       Match a single character in the range between “0” and “9” «[0-9]»
    #    Or match regular expression number 3 below (the entire group fails if this one fails to match) «3[01]»
    #       Match the character “3” literally «3»
    #       Match a single character present in the list “01” «[01]»
    # Assert position at a word boundary «\b»
    # Match any single character that is not a line break character «.+»
    #    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    # Assert position at the end of a line (at the end of the string or before a line break character) «$»
    

    UPDATE

    As long I understand, your input string contains many lines starts with a date but not contains commas in them. For this change the previous Pattern to this following:

    ^(19|20)\d\d([-/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])\s+\d+,[^,]+,[^,]+$
    

    or escaped

    ^(19|20)\\d\\d([-/.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])\\s+\\d+,[^,]+,[^,]+$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

guys i have arrays in which i have to match this kind of text
I have a CSV file I need to process which is a bit of
I have a kind of csv file, with some extra parameters. I don't want
I have an array which looks like this: array(10) { [0]=> string(10) 2012-11-03 [1]=>
I have this kind of question. In my form, i got this as a
I have this kind of node in my xml document: <parent ...> <a .../>
I have this kind of code below, how can I bind the visibility of
I have this kind of setup : Overridden BaseRunserverCommand that adds another option (--token)
I have this kind of table, it has many rows and each row has
I used to have this kind of simple JSON data and I could successfully

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.