Home > other >  Refer to two simple Python's title, thank you thank you
Refer to two simple Python's title, thank you thank you

Time:12-01

1. The input string, the string to satisfy the following requirements: contains two positive (possibly with decimal) and one operator (+ - */), operator among these Numbers, mingled with the indefinite number of other characters in a string,
2. Please calculate the string contains the value of the "math expression" output value rounded, keep a decimal places, # for example: # input: "gdfgdf234dg54gf * 23 op42" # output: "54929268" (for 23454 * 2342=54929268) # # input: "LKda withered; 2 asdewruli1ghjk salk1.23 o - P9rqew0. "# output:" - 89 "(since 1.23 to 90.21) # #

CodePudding user response:

 
The import re
# s="gdfgdf234dg54gf * 23 op42"
S="LKda withered; 2 asdewruli1ghjk salk1.23 o - P9rqew0. "
Match=re. The.findall (" [0-9 * + -/] + ", s)
Exp="'. Join (match)
Value=https://bbs.csdn.net/topics/eval (exp)
Print (" % 2 f "% value)


Explain:
1. Use regular expressions to find Numbers and operator, the result is that many a string group (groups)
2. Put the string into a string (join)
3. Use the eval () function to compute the string

CodePudding user response:

Forget to add a decimal point in the regular expression: [0-9 * + -/.]

CodePudding user response:

 import re 
While True:
STR=input (" should include one operator between two positive Numbers: ")
Input_regex=r '. * [0-9] + \. * [0-9] *. * * + -/] * [0-9] + \ [0-9] * *. * '
Search_out=re search (input_regex, STR)
If search_out:
Print (" valid input!" )
Break
Print (" invalid input! Re - input!" )

Cal_regex r='[0-9 \. * + -/] +'
Out_str="'. Join (re. The.findall (cal_regex, STR))
Print (' %. 1 f % eval (out_str))

CodePudding user response:

Square brackets "point" is not a wildcard, just "point" in itself, does not need to escape, i.e., [+ - */.] there is no "* + -/] [\."

CodePudding user response:

What ah, don't need to import under the condition of re, use python syntax, without using a library do

CodePudding user response:

Str1=input (' please enter the arithmetic expression: ')
Print (str1)
S='
For I in str1:
If I in [' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '. '] :
S=s + I
value=https://bbs.csdn.net/topics/eval (s)
Print (value)
Got a little trouble, don't have to load other libraries

CodePudding user response:

Consult the building Lord, has a string ABABA, ABA, the number of strings should be two, but I use a regular
 
STR="ABABA"
R=re.com running (" ABA ", re. M)
The back=r.f indall (" ABA ")
Print (back)///'ABA'

Can only find a ABA, regular should be how to write, we can find two ABA?

CodePudding user response:

refer to 7th floor cz19800823 response:
consult the building Lord, string ABABA, ABA number of string, there should be two, but I use a regular
 
STR="ABABA"
R=re.com running (" ABA ", re. M)
The back=r.f indall (" ABA ")
Print (back)///'ABA'

Can only find a ABA, regular should be how to write, we can find two ABA,


(your r.f indall (" ABA ") should be r.f indall (STR), changed, even if the result is the same)

Use Python's official document on re. The.findall:
"Return all non - overlapping matches of the pattern in the string, as a list of strings."
The.findall general search, returns the "non overlapping" series, you find a "overlap",

Can do it:
 import re 
Str1="ABABA"
(r=re.com running (r "?=(ABA))
")The back=r.f indall (str1)
Print (back)
  • Related