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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Splitting maths equation using regex

  • 05-02-2014 5: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,501 ✭✭✭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: 897 ✭✭✭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