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

  • Home
  • SEARCH
  • 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 7082225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:01:13+00:00 2026-05-28T07:01:13+00:00

I have a variable in the form of {varName} or {varName, defaultValue} and I

  • 0

I have a variable in the form of {varName} or {varName, “defaultValue”} and I want a regex that will match it. varName is only alphanumeric and “_” (\w+), and the default value can be anything except the combination of “} which signifies the end of the variable. White space doesn’t matter between the braces, comma, varName or defaultValue. So far the regex that I have come up with is

\{\s*(\w+)\s*(,\s*\"([^(\"\})]*)\"\s*)?\}

The problem is that the match ends at the first ” OR } and not the combination, i.e. {hello, “world”} does match but {hello, “wor”ld”} or {hello, “wor}ld”}

Any idea how to solve this? In case this helps, I’m coding it using Java.

  • 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-05-28T07:01:14+00:00Added an answer on May 28, 2026 at 7:01 am

    I have found the solution to my own question, it’s only fair to share. The following regex:

    \{\s*(\w+)\s*(,\s*\"((.*?)\s*\")?\})
    

    Will do the trick, stopping at the first sequence of ‘”}’

    In Java, this will be (continuing previous answer’s example):

    final Pattern p = Pattern.compile("\\{\\s*(\\w+)\\s*(,\\s*\"(.*?)\\s*\"\\s*)?\\}");
    Matcher m1 = p.matcher("{hello, \"world\"}");
    if (m1.matches()) {
        System.out.println("var1:" + m1.group(1));
        System.out.println("val1:" + m1.group(3));
    }
    Matcher m2 = p.matcher("{hello, \"wor\"rld\"}\"}");
    if (m2.matches()) {
        System.out.println("var2:" + m2.group(1));
    System.out.println("val2:" + m2.group(3));
    }
    
    /* Output
    var1:hello
    val1:world
    var2:hello
    val2:wor"rld"}
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem I want to display a form that will have a variable amount of
I have a variable that contains an array from form data, seen below: $option1
I have a form that has 8 columns and a variable number of rows
I have a variable that is posted through a html form: $_POST['ref'] And a
I have a variable that is definited by a POST call from a form
I have a function called taskButtonClick() that: Sets the variable form to a clone.
I'm in a Twig template, and I have a form variable that represents a
I have a variable $content that contains some text and images in this form
I have a variable called category that must change its value according to the
I have some variable where everytime the form is submiited or page is refreshed,

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.