Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Splitting maths equation using regex

  • 05-02-2014 05: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, Registered Users 2 Posts: 7,546 ✭✭✭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, Registered Users 2 Posts: 950 ✭✭✭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