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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:23:22+00:00 2026-06-17T21:23:22+00:00

I am having major trouble with understanding what is going wrong with a regex.

  • 0

I am having major trouble with understanding what is going wrong with a regex. To be complete I will lay out some background info.

This program is supposed to implement a SIMPLESEM interpreter. The grammar in concern here is this:

< Expr >    ==>   < Term > {( + | - ) < Term >}

< Term >    ==>   < Factor > {( * | / | % ) < Factor >}

< Factor >  ==>   < Number > | D[< Expr >] | (< Expr >)

< Number >  ==>   0 | (1..9){0..9}

I was provided with this code which is supposed to give me the contents inside the square brackets of a < Factor >, but it didn’t work:

 Matcher m;

(m = Pattern.compile("D\\[(.*)").matcher(expr)).find();
        expr = parseExpr(m.group(1));
        (m = Pattern.compile("\\](.*)").matcher(expr)).find();
        expr = m.group(1);

As example input i have this:

jumpt 5, D[0] == 0

The < Factor > concerned here is D[0]. It doesn’t work because the function above feeds 0] into parseExpr() which doesn’t handle the left over bracket, and it shouldn’t. So i switched it to:

(m = Pattern.compile("D\\[(.*)").matcher(expr)).find();
        expr = m.group(1);
        (m = Pattern.compile("\\](.*)").matcher(expr)).find();
        expr = parseExpr(m.group(1));

but this didn’t work because of the Matcher/Regex.
I believe it outputted an empty string.
So then I tried this which just gives me an error that there is no match:

(m = Pattern.compile("D\\[(.*)").matcher(expr)).find();
expr = m.group(1);

if(expr.contains("(.*)")) 
{
    (m = Pattern.compile("\\](.*)").matcher(expr)).find();
}
else
{
    (m = Pattern.compile("\\]").matcher(expr)).find();
}   
    expr = m.group(1);
    expr = parseExpr(expr);

It gives an index out of bounds at the second to last line.
Thanks in advance for your help.

  • 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-17T21:23:23+00:00Added an answer on June 17, 2026 at 9:23 pm

    There is this part D[ <Expr> ] and ( <Expr> ), which introduces the problem of bracket matching. This is not something Java regex can handle, since it doesn’t support recursive regex.

    In this case, regex is only useful for lexing, you need to write a custom parser for your language.

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

Sidebar

Related Questions

I'm having some major trouble deleting directories. I'm working building a ADMIN tool to
I'm having some trouble with writing some syntax. I want to echo this 'location_1'
Background I'm having trouble understanding the primary key choices in the database for a
This is giving me major headaches! I'm having trouble even describing the problem in
I am sure this question has been answered somewhere but I'm having major problems
Having trouble with each function... Will try to explain by example... In my code,
I am having some trouble updating UMDF drivers using devcon during a standard code-deploy-debug
I learned about multiprocessing from Python and I'm having a bit of trouble understanding
I am having major trouble selecting the most current/matching row in my database. For
I'm having trouble figuring out the best way to store what information my users

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.