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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:12:01+00:00 2026-06-06T06:12:01+00:00

EDIT: To explain my motivation for this, I’m writing a command-line utility that takes

  • 0

EDIT: To explain my motivation for this, I’m writing a command-line utility that takes a log file and a pattern (a non-regex string that indicates what each log entry looks like), converts the pattern into regex, and matches each line of the file with the regex, producing a collection of log events, which are then output in another format (e.g., JSON). I can’t assume what the input pattern will be or what the file contains.


I’d like to parse a CSV list of key-value pairs. I need to capture the individual keys and values from the list. An example input string:

07/04/2012 <DEBUG> a=1, b=foo, c=bar : hello world!\n

I verified that the regex below correctly extracts the keys and values from input:

// regex
(([^,\s=]+)=([^,\s=]+)(?:,\s*(?:[^,\s=]+)=(?:[^,\s=]+))*?)

// input string
a=1, b=foo, c=bar

The result is:

// 1st call
group(1) == "a"
group(2) == "1"

// 2nd call
group(1) == "b"
group(2) == "foo"

// 3rd call
group(1) == "c"
group(2) == "bar"

But this regex (same as regex above with extra “stuff”) does not work as expected:

// regex
\d{2}/\d{2}/\d{4} <DEBUG> (([^,\s=]+)=([^,\s=]+)(?:,\s*(?:[^,\s=]+)=(?:[^,\s=]+))*?) : .*

// input string
07/04/2012 <DEBUG> a=1, b=foo, c=bar : hello world! 

For some reason, the result is:

group(1) == "a=1, b=foo, c=bar"
group(2) == "a"
group(3) == "1"
// no more matches

What’s the correct Java regex to extract the keys and values?

  • 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-06T06:12:03+00:00Added an answer on June 6, 2026 at 6:12 am

    Regex:

    \d{2}/\d{2}/\d{4}\s<DEBUG>\s([^=]+)=([^,\s]+)[,\s]([^=]+)=([^,\s]+)[,\s]([^=]+)=([^\s]+)\s:.*
    

    Edit:
    If the count can be a arbitrary number, try the below one.

        Scanner s = new Scanner("07/04/2012 <DEBUG> a=1, b=foo, c=bar : d=erere  m=abcd hello world!");
        Pattern p = Pattern.compile("(?<=\\s|,)[^\\s=]+=[^,\\s]+");
        String out;
        while((out = s.findInLine(p))!=null) {
            System.out.println(Arrays.toString(out.split("=")));
        }
    

    Output:

    [a, 1]
    [b, foo]
    [c, bar]
    [d, erere]
    [m, abcd]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT (Side Question) Can someone please explain what this line does? eval website=\${$#} The
[EDIT] I am changing this to more concisely explain what my problem was, after
EDIT I added this note to explain why I keep this question here. I
EDIT following resolution, this has been substantially edited to dump irrelevant detail and explain
This is hard to explain, so let me just give you a link: http://jsbin.com/izinoy/6/edit#html,live
EDIT: You can see the search box live here . Before I explain, let
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
Please explain to me how this JavaScript row works: <a href=javascript: onclick=document.location='AddItem.aspx?catid=<%# Eval(CollectionID)%>'>[edit items]</a>
This is a query that totals up every players game results from a game
EDIT: Sometimes it happens that our problem is kind of not in code but

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.