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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:11:37+00:00 2026-06-06T14:11:37+00:00

I’m trying to write a regex/method that extracts Variables from an input String that

  • 0

I’m trying to write a regex/method that extracts Variables from an input String that represents “math/algebraic expression” and a special pattern that looks like this “PROPERTY(AnyOtherAplhaNumeric)” which can also be a variable.

My definition of a variable:

1) Can contain alphanumeric values only

2) Must be at least 1 char or more

3) Cannot start with a digit , must start with [A-Za-z]

4) A variable for example “X”, can be surrounded by this string “PROPERTY(X)”, therefore the variable becomes “PROPERTY(X)”

My current Method & Regex (works only in some cases):

public Set<String> extractUniqueVarsFromExpression(String expression) {
        Set<String> varsSet = null;
        Pattern p = null;
        Matcher m = null;
        System.out.println(expression);
        if (expression != null) {
            varsSet = new java.util.LinkedHashSet<String>();
            //"[A-Za-zPROPERTY(?)_][A-Za-z0-9PROPERTY(?)_]*||[A-Za-z_][A-Za-z0-9_]*"
            //"[[A-Za-z_][A-Za-z0-9_]*"
            p = Pattern.compile("[A-Za-zPROPERTY(?)_][A-Za-z0-9PROPERTY(?)_]*||[A-Za-z_][A-Za-z0-9_]*",
                    Pattern.CASE_INSENSITIVE);
            m = p.matcher(expression);
            while (m.find()) {

                    String group = m.group().trim();
                //do not add duplicates     

                         if (!varsSet.contains(group)) 
                         {
                    varsSet.add(group);

                    System.out.println(" Variable : " + group);
                         }//end if not duplicate 

            }// end while

        }
        System.out.println();
        return varsSet;
    }

Examples/Cases:

Ex #1:

Input: [(ibdweight / ibdheight) * ibdheight] * 703

Output:

Variable : PROPERTY(ibdweight)

Variable : PROPERTY(ibdheight)

Ex #2:

Input: [ibdweight / ibdheight * ibdheight] * 703

Output:

Variable : ibdweight

Variable : ibdheight

Ex #3:

Input: [PROPERTY(ibdweight) / [PROPERTY(ibdheight) * PROPERTY(ibdheight)] * 703

Output:

Variable : PROPERTY(ibdweight)

Variable : PROPERTY(ibdheight)

Ex #:4

These are the cases that don’t work (examples 4 to 6):

The problem is the parenthesis are being picked up as variables:

Input: ( Mass * ( Acceleration + whatever ))

Output:

Variable : (

Variable : Mass

Variable : Acceleration

Variable : whatever

Variable : ))

Ex #:5

The problem is the parenthesis are being picked up as variables:

Input: ( Base * Height ) / 2

Output:

Variable : (

Variable : Base

Variable : Height

Variable : )

Ex #:6

The problem is the parenthesis are being picked up as variables OR attached to a variable:

Input: [((( var * var2 ) var3 ) + ( var1 / var4 ) var5) / var6 ]

Output:

Variable : (((

Variable : var

Variable : var2

Variable : )

Variable : var3

Variable : (

Variable : var1

Variable : var4

Variable : var5)

Variable : var6

  • 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-06T14:11:38+00:00Added an answer on June 6, 2026 at 2:11 pm

    The problem with your regular expression is that you have the both the parentheses and the word “PROPERTY” inside of the brackets. Brackets are for specifying a set of characters, not strings, any member of which will match.

    A simple (although probably not optimal) variation that should work for you is:
    (PROPERTY\([A-Za-z][A-Za-z0-9_]*\))|([A-Za-z][A-Za-z0-9_]*)

    A slightly better version would be:
    (PROPERTY\([A-Za-z]\w*\))|([A-Za-z]\w*)

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

Sidebar

Related Questions

I'm trying to write a regular expression for Java's String.matches(regex) method to match a
I am trying to write a regex expression for identifying alphabets in a string.
i am trying to write a regex that produces the content in a string
I'm trying to write regex that will split a string when there is whitespace
I am trying to write a regex that will extract a tweet id from
I'm trying to write a regex expression that will take this phrase [[32, 120],
I'm trying to write a regex that will take a string of the form:
I am trying to write a regex string to match a string revived from
I'm trying to write a Regex that that will extract individual fields from a
I'm trying to write a regex expression to match the src, width and height

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.