Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Splitting maths equation using regex

Options
  • 05-02-2014 6:03pm
    #1
    Closed Accounts Posts: 2,113 ✭✭✭


    I'm stumped trying to split a maths equation containing negative numbers using regex in Java. I want to be able to split a negative number into the one String rather than into separate '-' and '3' Strings.

    Sample equation:
    (-3)+4*2.5/(1-2.5)^2^3

    I'm using this to split the equation:
    String[] tokens = inputString
    			.split("(?<=[-+*/=^()])|((?<=-)(?!\\d))|(?=[()\\-+*/=^])");
    

    And the result I'm getting when I print the array is:
    (, -, 3, ), +, 4, *, 2.5, /, (, 1, -, 2.5, ), ^, 2, ^, 3

    Any help would be appreciated.


Comments

  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    I'm stumped trying to split a maths equation containing negative numbers using regex in Java. I want to be able to split a negative number into the one String rather than into separate '-' and '3' Strings.

    Sample equation:
    (-3)+4*2.5/(1-2.5)^2^3

    I'm using this to split the equation:
    String[] tokens = inputString
    			.split("(?<=[-+*/=^()])|((?<=-)(?!\\d))|(?=[()\\-+*/=^])");
    

    And the result I'm getting when I print the array is:
    (, -, 3, ), +, 4, *, 2.5, /, (, 1, -, 2.5, ), ^, 2, ^, 3

    Any help would be appreciated.

    I think this might work.

    "(?<=[-/d][+*/=^()])|((?<=-)(?!\\d))|(?=[()\\-+*/=^])"


  • Registered Users Posts: 869 ✭✭✭moycullen14


    You sure that's what you want?

    I guess you're trying to create some sort of evaluator for the expressions? Something like

    left hand expression, operator, right hand expression

    where expression could be a literal (1,2,3,4, etc) or an expression (e.g. 1 + 2).
    -3 is not a literal, it's an expression. Think of it is 0 - 3. Similarly +3 is an expression.

    What you probably need is a recursive descent parser. Have a look at javacc.

    It might be overkill but it's really what you want.

    Alternatively,

    What you are doing here is trying to tokenize some input. The tokens you have are literals(1,2,3), operators (+,-,*,/) and expression markers ( and ).

    Try splitting the string up into tokens based on that. Use StringTokenizer,

    e.g.

    StrinkTokenizer st = new StringTokenizer(s,"+-/*() \t",true);

    Parsing arithmetic equations using regular expressions will end in tears. Can you cope with
    + 3 - 4, *8 / +7, etc?


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    I think this might work.

    "(?<=[-/d][+*/=^()])|((?<=-)(?!\\d))|(?=[()\\-+*/=^])"
    This groups all numbers with it's preceding operator, which wasn't what I wanted.

    I managed to get what I wanted with this though:

    "(?<=[\\(d-d]|[+\\)-d][-+*/%=^()])|((?<=-)(?!\\d))|(?=[()\\-+*/%=^])"

    Not pretty, I know.
    You sure that's what you want?
    For what I'm doing I need to split the equations up into separate number and operator tokens. There's probably easier ways of doing it but I prefer something short and sweet.


Advertisement