Python Forum
i need a more complex regexp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need a more complex regexp
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020