I have a string in python, which开发者_如何学Python is in this format:
[NUMBER][OPERATOR][NUMBER][UNNEEDED JUNK]
e.g.:
5+5.[)]1
How could I trim that down to just 5+5
?
EDIT
I forgot to mention, basically, you just need to look for the first non-numeric character after the operator, and crop everything (starting at that point) off.
This is a simple regular expression:
import re
s = "5+5.[)]1"
s = re.search("\d+\+\d+", s).group()
print(s) # 5+5
re.search(r'\d+.\d+','123+55.[)]1').group()
This should work.
精彩评论