Python Forum
i need a more complex regexp - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: i need a more complex regexp (/thread-21628.html)



i need a more complex regexp - Skaperen - Oct-08-2019

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?


RE: i need a more complex regexp - Gribouillis - Oct-08-2019

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.


RE: i need a more complex regexp - Skaperen - Oct-08-2019

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.


RE: i need a more complex regexp - Gribouillis - Oct-09-2019

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.


RE: i need a more complex regexp - Skaperen - Oct-10-2019

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.