开发者

Python string trimming

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜