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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:01:03+00:00 2026-06-16T15:01:03+00:00

I am attempting to create a parser for java expressions, but for some reason

  • 0

I am attempting to create a parser for java expressions, but for some reason I am unable to match floating point values. I am using a java.util.Matcher obtained from

Matcher token = Pattern.compile(
        "(\\w[\\w\\d]*+)|" + //identifiers as group 1
        "((?:(?>[1-9][0-9]*+\\.?[0-9]*+)|(?>\\.[0-9]++))(?:[Ee][+-]?[0-9]++)?)|" + //literal numbers
        "([^\\w\\d\\s]*+)" //operators as group 3
    ).matcher();

This is intended to match an identifier, a floating point value, or an operator (I still need to refine that part of the match though will refine that part of the match later). However, I am having an issue with it in that

Below is the code that is using that expression, which is intended to take all the identifiers, numbers, and operators, register all the numbers in vars, and put all the identifiers, each number’s corresponding value, and all the operators in tokens in same order as in the original string.

It does not succeed in doing so, however, because for an input string like foo 34.78e5 bar -2.7 the resulting list is ‘[34, A, , bar, , -, 2, B, ]’ with A=-78000.0 and B=-0.7. It is supposed to return ‘[foo, A, bar, B]` with A=3478000 and B=-2.7. I beleive it may be just that it is failing to include both parts of the number as the match of the regex, however that may not be the case.

I have tried removing the atomic grouping and possesives from the regex, however that did not change anything.

LinkedList<String> tokens = new LinkedList<String>();
HashMap<String, Double> vars = new HashMap<String, Double>();
VariableNamer varNamer = new VariableNamer();

for(Matcher token = Pattern.compile(
                        "(\\w[\\w\\d]*+)|" + //variable names as group 1
                        "((?:(?:[1-9][0-9]*+\\.?[0-9]*+)|(?:\\.[0-9]++))(?:[Ee][+-]?[0-9]++)?)|" +
                                             //literal numbers as group 2
                        "([^\\w\\d\\s]*+)"   //operators as group 3
                ).matcher(expression); token.find();){

        if(token.group(2) != null) { //if its a literal number, register it in vars and substitute a string for it
            String name = varNamer.next();

            if (
                    tokens.size()>0 &&
                    tokens.get(tokens.size()-1).matches("[+-]") &&
                    tokens.size()>1?tokens.get(tokens.size()-2).matches("[^\\w\\d\\s]"):true
                    )

                vars.put(name, tokens.pop().equals("+")?Double.parseDouble(token.group()):-Double.parseDouble(token.group()));
            else
                vars.put(name, Double.parseDouble((token.group())));

            tokens.addLast(name);
        } else {
            tokens.addLast(token.group());
        }
    }

and here is VariableNamer:

import java.util.Iterator;

public class VariableNamer implements Iterator<String>{

    StringBuffer next = new StringBuffer("A");

    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public String next() {
        try{
            return next.toString();
        }finally{
            next.setCharAt(next.length()-1, (char) (next.charAt(next.length()-1) + 1));

            for(int idx = next.length()-1; next.charAt(idx) + 1 > 'Z' && idx > 0; idx--){
                next.setCharAt(idx, 'A');
                next.setCharAt(idx - 1, (char) (next.charAt(idx - 1) + 1));
            }

            if (next.charAt(0) > 'Z'){
                next.setCharAt(0, 'A');
                next.insert(0, 'A');
            }
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

}
  • 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-16T15:01:04+00:00Added an answer on June 16, 2026 at 3:01 pm

    Depending on details of your expression mini-language, it is either close to the limit on what is possible using regexes … or beyond it. And even if you do succeed in “parsing”, you will be left with the problem of mapping the “group” substrings into a meaningful expression.

    My advice would be to take an entirely different approach. Either find / use an existing expression library, or implement expression parsing using a parser generator like ANTLR or Javacc.

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

Sidebar

Related Questions

I attempting to create custom tabs using this . But when I try to
I'm attempting to create a table for monitoring purposes using the following script: $w3wppriv
I'm attempting to create a cascading dropdown based on the Knockout.js Cart example but
So, I'm attempting to create a .txt file, and then write some silly data
I am attempting to create an html document parser with Python. I am very
I am attempting a basic recursion to create multi-dimensional arrays based on the values
I am attempting to create a custom messaging app. Just something basic but I
Some background: I am attempting to create a DirectShow source filter based on the
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
Im attempting to create a new operator :? on lists, which operates the same

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.