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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:56:32+00:00 2026-06-09T18:56:32+00:00

I am using a regular expression to extract key-value pairs from arbitrarily long input

  • 0

I am using a regular expression to extract key-value pairs from arbitrarily long input strings and have run into a case in which, for a long string with repetitive patterns, it causes a stack overflow.

The KV-parsing code looks something like this:

public static void parse(String input)
{
    String KV_REGEX = "((?:\"[^\"^ ]*\"|[^=,^ ])*) *= *((?:\"[^\"]*\"|[^=,^\\)^ ])*)";
    Pattern KV_PATTERN = Pattern.compile(KV_REGEX);

    Matcher matcher = KV_PATTERN.matcher(input);

    System.out.println("\nMatcher groups discovered:");

    while (matcher.find())
    {
        System.out.println(matcher.group(1) + ", " + matcher.group(2));
    }
}

Some fictitious examples of output:

    String input1 = "2012-08-09 09:10:25,521 INFO com.a.package.SomeClass - Everything working fine {name=CentOS, family=Linux, category=OS, version=2.6.x}";
    String input2 = "2012-08-09 blah blah 09:12:38,462 Log for the main thread, PID=5872, version=\"7.1.8.x\", build=1234567, other=done";

Calling parse(input1) produces:

{name, CentOS
family, Linux
category, OS
version, 2.6.x}

Calling parse(input2) produces:

PID, 5872
version, "7.1.8.x"
build, 1234567
other, done

This is fine (even with a bit of string processing required for the first case). However, when trying to parse a very long (over 1,000 characters long) classpath string, the aforementioned class overflow occurs, with the following exception (start):

Exception in thread "main" java.lang.StackOverflowError
    at java.util.regex.Pattern$BitClass.isSatisfiedBy(Pattern.java:2927)
    at java.util.regex.Pattern$8.isSatisfiedBy(Pattern.java:4783)
    at java.util.regex.Pattern$8.isSatisfiedBy(Pattern.java:4783)
    at java.util.regex.Pattern$8.isSatisfiedBy(Pattern.java:4783)
    at java.util.regex.Pattern$8.isSatisfiedBy(Pattern.java:4783)
    at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
    ...

The string is too long to put here, but it has the following, easily reproducible and repetitive structure:

java.class.path=/opt/files/any:/opt/files/any:/opt/files/any:/opt/files/any

Anyone who wants to reproduce the issue just needs to append :/opt/files/any a few dozen times to the above string. After creating a string with about 90 copies of “:/opt/files/any” present in the classpath string, the stack overflow occurs.

Is there a generic way that the above KV_REGEX string could be modified, so that the issue does not occur and the same results are produced?

I explicitly put generic above, as opposed to hacks that (for instance) check for a maximum string length before parsing.

The most gross fix I could come up with, a true anti-pattern, is

public void safeParse(String input)
{
    try
    {
        parse(input);
    }
    catch (StackOverflowError e) // Or even Throwable!
    {
        parse(input.substring(0, MAX_LENGTH));
    }
}

Funnily enough, it works in a few runs I tried it, but it is not something tasteful enough to recommend. 🙂

  • 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-09T18:56:33+00:00Added an answer on June 9, 2026 at 6:56 pm

    Your regex looks overly complicated, for example I think you haven’t quite understood how character classes work. This works better for me, I can’t make it overflow anymore:

    public static void parse(String input) {
        String KV_REGEX = "(\"[^\" ]*\"|[^{=, ]*) *= *(\"[^\"]*\"|[^=,) }]*)";
        Pattern KV_PATTERN = Pattern.compile(KV_REGEX);
    
        Matcher matcher = KV_PATTERN.matcher(input);
    
        System.out.println("\nMatcher groups discovered:");
    
        while (matcher.find()) {
            System.out.println(matcher.group(1) + ", " + matcher.group(2));
        }
    }
    

    To break down the regex, this will match:

    (\"[^\" ]*\"|[^{=, ]*): Anything enclosed with "s, or any number of non-{=, characters

    *= *: zero to any number of spaces, followed by =, followed by zero to any number of spaces

    (\"[^\"]*\"|[^=,) }]*): Anything enclosed with "s, or any number of non-=,) } characters

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

Sidebar

Related Questions

Using PHP Regular Expression I want to extract some value from code <?xml version=1.0
I'm trying to extract/match data from a string using regular expression but I don't
I need to extract the digits from the following string using regular expression: pc
I have a following value 1pm 2am that I'm using a regular expression againts
I need to extract URPlus1_S2_3 from the string: Last one: http://abc.imp/Basic2#URPlus1_S2_3, using regular expression
How can I extract the string XMLFileName from the below URL using regular expression
How can I extract the word following text: using a regular expression? The input
I'm trying to extract UA-123456-7 from the following Google Analytic using regular expression. I
I would like to extract portion of a text using a regular expression. So
I want to extract abc from images/abc.png using Javascript Regular experssion in one line.

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.