regex - Python & regular expressions -


Hi I am creating a program that is ! Math (5 + 5) Get more results from Skype, I'm having trouble getting the Résx expression correct, I'm reading docs and I can not get right now

I I'm trying to support operators, so , * /, +, -,%, and ** trouble occurs when I try to use **, either I I use a regex and lose exponent options or get exponent option is.

Here's my expression:

  Expr = re.search (r '((\ d +) (\ s *.?. *. * \ **) (\ D +), 'Mathematics (500 ** 1000)')  

And then I am parse it using the group,

  Build_expr = {'number1': int (expr.group (2)), 'operator': expr.group (3), 'number2': int (XPRGP (4))}  

Results of logging leakage modules with exponents:

  & gt; & Gt; & Gt; Expr.group () '500 ** 1000' & gt; & Gt; & Gt; Expr.group (1) '500 ** 1000' & gt; & Gt; & Gt; Expr.group (2) '500' & gt; & Gt; & Gt;  Mathematics (500 + 1000)      / P> 
  & Gt; & Gt; & Gt; Expr.group (1) '500 + 1000' & gt; & Gt; & Gt; Expr.group (2) '500' & gt; & Gt; & Gt; Expr.group (3) '+' & gt; & Gt; & Gt; Expr.group (4) '1000'  

Here is the whole function

  def math_func (expr_arg): expr = re.search ('r' (( \ D +) (\ s *.? ... * \ **) (, \ d +)), expr_arg #parse_expr = '' .join (expr.group ()). Split () build_expr = {'number1': Int (expr.group (2)), 'operator': expr.group (3), 'number2': int (expr.group (4))} if build_expr ['operator' ']': Operation = Build_expr ['number1'] + build_expr ['number2'] Return str (operation) aliff build_xp ['operator'] == '-': operation = build_xprro [[number1 '] - Operation = build_xprro [[number1]] / build_exprar ['no2'] Return str (Operation) ['Number 2'] Return Str (Operation) Alief Build_Exper ['Operator'] == '/' ) Alief Build_XP ['Operator'] == '%': Operation = Build_ Expression ['Number1']% build_expr ['number2'] Return Str (Operation) Alief Build_Express ['Operator'] == '*': Operation = Build_XP ['Number1'] * Build_XP Return Str (Operation) Alif Return_express ['operator'] == '**': Operation = build_xpr ['no1'] ** build_xp ['no2'] return str (operation) Other: return 'invalid operator' return 'all good son' F = math_func ('Mathematics (500 + 1000)' keeps silent) Message. Chat Message message ('> & gt;' + F)   

can be matched using Is:

  (\ d +) \ s * ([- + * /%] +) \ s * (\ d +)  

breakdown

  • will match one or more digits
  • \ s * will match white space, if any <
  • ([- + * /%] +) will match one or more operator characters

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -