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

The Archive Base Latest Questions

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

I want to learn how to use regular expressions in Java and found the

  • 0

I want to learn how to use regular expressions in Java and found the following tasks:
Write a class to check whetever a given input string is a valid arithmetic term based on these criteria in BNF-form:

term = [Vz]summand|[Vz]summand addOp term
summand = factor | factor mulOp summand
factor = number | '('term')'
number = digit | digit number
digit = '0'|'1'|...|'9'
vz = '+'|'-'
addOp = '+'|'-'
mulOp = '*'|'/'

Using these rules, I wrote some patterns, resembling the different types:

static Pattern vz = Pattern.compile("[+-]");
static Pattern addOp = Pattern.compile("[+-]");
static Pattern multOp = Pattern.compile("[*/]");
static Pattern digit= Pattern.compile("[0-9]");
static Pattern number = Pattern.compile(digit.pattern()+"+");
static Pattern factor = Pattern.compile(number.pattern()+"|("+term.pattern()+")");
static Pattern summand = Pattern.compile(factor.pattern()+"|"+factor.pattern()+ multOp.pattern()+"\n");
static Pattern term = Pattern.compile(vz.pattern()+"?"+summand.pattern()+"|"
        +vz.pattern()+"?"+summand.pattern()+addOp.pattern()+"\n");

And you already see my problem: I reference term in the definiton of factor without having it defined first. Unfortunately I can not switch it around in any way. So my question is:

Is there a possibility to reference a pattern in such a way? Or any other to reference a pattern and defining it later on?

  • 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-15T17:13:47+00:00Added an answer on June 15, 2026 at 5:13 pm

    The problem is, BNF defines a context-free grammar (which describes languages more complex than those described by regular expressions). You will have to come up with a different approach than just using the BNF rules as regex patterns straight away.

    In particular, correct nesting of parentheses is not regular. Some regex engines support (non-regular) features that allow for matching of these, but the regexes often become very long and unmaintainable. And I’m not sure right now if Java has any of these features (PCRE and .NET do, for instance).

    If you want to solve the task at hand, you will have to write a parse manually. If you want to learn regular expressions, you will have to either do it in another language or look for a different task. However, here is a great source to improve your regex skills.

    For the fun of it (and to show you why regular expressions are not the right tool for this, even if the engine supports the necessary features), here is the regular expression that corresponds to the above BNF (except for the Vz rule, for some odd reason I could not get it to work):

    ^(((\d+|[(](?1)[)])|(?3)[*\/](?2))|(?2)[+-](?1))$
    

    The (?n) recursively try to match the nth subpattern (which are counted by opening parentheses from left to right).

    It does not work in PHP, but I believe their PCRE implementation has some backtracking issues when using recursion. An online PCRE tester seemed to treat some example input correctly. Here it is in free-spacing mode (x) with some annotations:

    ^
    (                # term (?1)
      (              # summand (?2)
        (            # factor (?3)
          \d+        # number
        |
          [(](?1)[)] # (term)
        )            # end of factor
      |
        (?3)[*/](?2) # factor mulOp summand
      )              # end of summand
    |
      (?2)[+-](?1)   # summand addOp term
    )                # end of term
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Common Lisp and want to use regular expressions for parsing
I want to learn and use Drupal or Django for the following: dynamic web
I'm starting to learn Regular Expressions and I want to know: In which cases
I am new to Java. I want to learn to use GZIPstreams. I already
I want to learn how to use MSBuild ReadLinesFromFile Task I observe Output tag
If you want to learn how to use Perl interpreter threads, there's good documentation
I made a tic-tac-toe game in c++ want to learn how to use SDL
I want to learn zend without using the zend tool. I've found some good
I want to learn how to use JGoodies binding (since beans binding seems dead
I want to learn and use SBCL because of its ease of learning and

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.