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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:13:54+00:00 2026-06-15T00:13:54+00:00

File structure is as such: group,type,scope,name,attribute,value c,,Probes Count,Counter,value,35 b,ProbeInformation,Probes Count,Gauge,value,0 Always using quotes. There

  • 0

File structure is as such:

"group","type","scope","name","attribute","value"
"c","","Probes Count","Counter","value","35"
"b","ProbeInformation","Probes Count","Gauge","value","0"

Always using quotes. There is a trailing newline as well.

Here is what I have:

^(\"[^,\"]*\")(,(\"[^,\"]*\"))*(.(\"[^,\"]*\")(,(\"[^,\"]*\")))*.$

That is not matching correctly. I’m using String.matches(regexp);

  • 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-15T00:13:55+00:00Added an answer on June 15, 2026 at 12:13 am

    Disclaimer: I didn’t even try compiling my code, but this pattern has worked before.

    When I can’t see at a glance what a regex does, I break it out into lines so it’s easier to figure out what’s going on. Mismatched parens are more obvious and you can even add comments to it. Also, let’s add the Java code around it so escaping oddities become clear.

    ^(\"[^,\"]*\")(,(\"[^,\"]*\"))*(.(\"[^,\"]*\")(,(\"[^,\"]*\")))*.$
    

    becomes

    String regex = "^" +
                   "(\"[^,\"]*\")" +
                   "(," +
                     "(\"[^,\"]*\")" +
                   ")*" +
                   "(." +
                     "(\"[^,\"]*\")" +
                     "(," +
                        "(\"[^,\"]*\")" +
                     ")" +
                   ")*" +
                   ".$";
    

    Much better. Now to business: the first thing I see is your regex for the quoted values. It doesn’t allow for commas within the strings – which probably isn’t what you want – so let’s fix that. Let’s also put it in its own variable so we don’t mis-type it at some point. Lastly, let’s add comments so we can verify what the regex is doing.

    final String QUOTED_VALUE = "\"[^\"]*\""; // A double quote character, zero or more non-double quote characters, and another double quote
    String regex = "^" +                           // The beginning of the string
                   "(" + QUOTED_VALUE + ")" +      // Capture the first value
                   "(," +                          // Start a group, a comma
                     "(" + QUOTED_VALUE + ")" +    // Capture the next value
                   ")*" +                          // Close the group.  Allow zero or more of these
                   "(." +                          // Start a group, any character
                     "(" + QUOTED_VALUE + ")" +      // Capture another value
                     "(," +                            // Started a nested group, a comma
                        "(" + QUOTED_VALUE + ")" +     // Capture the next value
                     ")" +                             // Close the nested group
                   ")*" +                            // Close the group.  Allow zero or more
                   ".$";                           // Any character, the end of the input
    

    Things are getting even clearer. I see two big things here:

    1) (I think) you’re trying to match the newline in your input string. I’ll play along, but it’s cleaner and easier to split the input on a newline than what you’re doing (that’s an exercise you can do yourself though). You also need to be mindful of the different newline conventions that different operating systems have (read this).

    2) You’re capturing too much. You want to use non-capturing groups or parsing your output is going to be difficult and error-prone (read this).

    final String QUOTED_VALUE = "\"[^\"]*\""; // A double quote character, zero or more non-double quote characters, and another double quote
    final String NEWLINE = "(\n|\n\r|\r\n)";  // A newline for (almost) any OS: Windows, *NIX or Mac
    String regex = "^" +                           // The beginning of the string
                   "(" + QUOTED_VALUE + ")" +   // Capture the first value
                   "(?:," +                       // Start a group, a comma
                     "(" + QUOTED_VALUE + ")" + // Capture the next value
                   ")*" +                       // Close the group.  Allow zero or more of these
                   "(?:" + NEWLINE +            // Start a group, any character
                     "(" + QUOTED_VALUE + ")" +   // Capture another value
                     "(?:," +                       // Started a nested group, a comma
                        "(" + QUOTED_VALUE + ")" +  // Capture the next value
                     ")" +                          // Close the nested group
                   ")*" +                         // Close the group.  Allow zero or more
                   NEWLINE + "$";                 // A trailing newline, the end of the input
    

    From here, I see you duplicating work again. Let’s fix that. This also fixes a missing * in your original regex. See if you can find it.

    final String QUOTED_VALUE = "\"[^\"]*\""; // A double quote character, zero or more non-double quote characters, and another double quote
    final String NEWLINE = "(\n|\n\r|\r\n)";  // A newline for (almost) any OS: Windows, *NIX or Mac
    final String LINE = "(" + QUOTED_VALUE + ")" +   // Capture the first value
                        "(?:," +                       // Start a group, a comma
                          "(" + QUOTED_VALUE + ")" + // Capture the next value
                        ")*";                        // Close the group.  Allow zero or more of these
    String regex = "^" +             // The beginning of the string
                   LINE +            // Read the first line, capture its values
                   "(?:" + NEWLINE + // Start a group for the remaining lines
                     LINE +            // Read more lines, capture their values
                   ")*" +            // Close the group.  Allow zero or more
                   NEWLINE + "$";    // A trailing newline, the end of the input
    

    That’s a little easier to read, no? Now you can test your big nasty regex in pieces if it doesn’t work.

    You can now compile the regex, get the matcher, and grab the groups from it. You still have a few issues though:

    1) I said earlier that it would be easier to break on newlines. One reason is: how do you determine how many values do you have per line? Hard-coding it will work, but it’ll break as soon as your input changes. Maybe this isn’t a problem for you, but it’s still bad practice. Another reason: the regex is still too complex for my liking. You could really get away with stopping at LINE.

    2) CSV files allow lines like this:

    "some text","123",456,"some more text"
    

    To handle this you might want to add another mini-regex that gets either a quoted value or a list of digits.

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

Sidebar

Related Questions

Have such .proto file structure { session{ field1 = value; field2 = value; ...
I have to parse a 1Gb XML file with a structure such as below
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...
The problem is I've got xml file with such structure ...................... <current_conditions> <condition data=partly
I have an xml file with a parent child structure such as : <geometry
There's a file with such structure: companyname1;shortname1;identification1;01.01.1980 companyname2;shortname2;identification2;01.01.1987 companyname3;shortname3;identification3;01.01.1990 companyname4;shortname4;identification4;23.01.1995 I want to put
In this header file, I am getting error: unknown type name uint32, uint16. I
I have text file with such structure: Some data ==================================================================== 1 !V09 ! -0.544
I've got an xml file with such structure: <panel> <tr> <td> <element> ... smth
Say I have a file based data structure such as a B+ Tree. My

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.