Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

inconsistent matching with arithmetic expression

Last post 10-07-2008, 3:13 PM by prometheuzz. 8 replies.
Sort Posts: Previous Next
  •  10-03-2008, 4:56 AM 46845

    inconsistent matching with arithmetic expression

    hi all,

    i'm working on a regular expression that will match an arithmetic expression like 3+4/5, with valid operators being -,+,*,/ without negative numbers and decimal numbers being either abc. or .abc or abc.def in form. i first match the first number and then match the rest of the expression by capturing a pair: one operator then any number of digits. i'm currently working with php and my ultimate goal is to validate the string and evaluate the arithmetic expression.

    /[+-\/*]\d+(\.\d*)?/

    is my regular expression within preg_match and it seems to work for the most part. i'm assuming that the character class [+-\/*] will always match one of the four operators once and only once.

    preg_match("/[+-\/*]\d+(\.\d*)?/", $str, $words, PREG_OFFSET_CAPTURE, $index);

    but when i actually run the program, i find that the regular expression sometimes doesn't capture the operator with this test case: 89067/8+0.9+.8+9. (including the trailing decimal/period)

    Array ( [0] => 89067 [1] => 0 )
    Array ( [0] => Array ( [0] => /8 [1] => 5 ) )
    Array ( [0] => Array ( [0] => +0.9 [1] => 7 ) [1] => Array ( [0] => .9 [1] => 9 ) )
    Array ( [0] => Array ( [0] => .8 [1] => 12 ) )
    Array ( [0] => Array ( [0] => +9. [1] => 14 ) [1] => Array ( [0] => . [1] => 16 ) ) 

    notice that the second to last line has the expression matching ".8" with the operator omitted. is there something i'm missing?

  •  10-03-2008, 5:43 AM 46847 in reply to 46845

    Re: inconsistent matching with arithmetic expression

    This will validate such expressions:

    '#^(\d+\.\d+|\.\d+|\d+\.|\d+)([-+/*](\d+\.\d+|\.\d+|\d+\.|\d+))*$#'

    Note that inside a character class, to match a hyphen, it either has to be the first or the last inside the class:

    [-ac]   // matches '-', 'a' or 'c'
    [a-c]   // matches 'a', 'b' or 'c'
    [ac-]   // matches 'a', 'c' or '-'

    HTH

  •  10-03-2008, 11:06 AM 46857 in reply to 46847

    Re: inconsistent matching with arithmetic expression

    prometheuzz:

    Note that inside a character class, to match a hyphen, it either has to be the first or the last inside the class:

    [-ac]   // matches '-', 'a' or 'c'
    [a-c]   // matches 'a', 'b' or 'c'
    [ac-]   // matches 'a', 'c' or '-'

    HTH

    Or you can escape the hyphen. Using hex notation works works too.

     
    [a-c]   // matches 'a', 'b' or 'c'

    [a\-c]  // matches 'a', '-' or 'c'

    [a\x2dc]   // matches 'a', '-' or 'c'


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  10-03-2008, 11:11 AM 46858 in reply to 46845

    Re: inconsistent matching with arithmetic expression

    Out of curiosity, why are you taking this approach instead of a more traditional parsing approach that respects the order of operations and allows for parentheses?
  •  10-04-2008, 10:48 PM 46881 in reply to 46858

    Re: inconsistent matching with arithmetic expression

    hey everyone, to answer your question lyndar, we're not required to take care of parenthetical expressions and we only need to be able to parse arithmetic expressions containing the four operators i listed before.

    i took the advice of placing the hyphen in the front of the character set and it worked perfectly! 

  •  10-07-2008, 1:27 PM 46973 in reply to 46881

    Re: inconsistent matching with arithmetic expression

    So what was the final working expression?
  •  10-07-2008, 1:38 PM 46974 in reply to 46973

    Re: inconsistent matching with arithmetic expression

    and also, what would the expression be to handle negative and decimal numbers?
  •  10-07-2008, 3:10 PM 46975 in reply to 46973

    Re: inconsistent matching with arithmetic expression

    ddawg:
    So what was the final working expression?

    Didn't you try the things/regexes that were suggested in this thread?

  •  10-07-2008, 3:13 PM 46976 in reply to 46974

    Re: inconsistent matching with arithmetic expression

    ddawg:
    and also, what would the expression be to handle negative

    Put an optional minus sign in front of the number.

    ddawg:
    and decimal numbers?

    My suggestion already does.

View as RSS news feed in XML