Python Forum

Full Version: i need a more complex regexp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i need a more complex regular expression i hope to use with module re. in this expression i need to match 2 (and in 2 more variation, 3 and 4) arithmetic expressions separated by any character that is not valid in an arithmetic expression. i plan to evaluate the arithmetic expressions with the eval() function to get their values. the strings are coming from safe places, like input from the user that runs it. is there a symbol in PCRE to do this or some combination that can do it?
Writing a regular expression is all about describing more accurately the expression that you want to match. Also a problem with arithmetic expressions is that they contain arbitrary levels of parentheses. A small parser may be better suited.
would eval() be usable? i could test the result type. an expression like int('1'+'2') would end up being valid, but that's actually OK.
If you could cut the expression on the character that is not valid in an arithmetic expression, you could parse the different pieces with the ast module, provided the arithmetic expressions follow python's syntax. Alternatively, you could replace the invalid character by an operator with low precedence such as 'or' and parse directly the whole expression with the ast module, provided that none of the arithmetic expressions uses the 'or' operator.
i can define my input rules as "arithmetic expressions valid in python". i can run a loop over a decreasing range from the length of the string down to 1 and try a slicing of the string from longest to shortest and if any succeed break out and i will have the longest valid expression. "or" might possibly be useful in the expression.