开发者

Python string trimming

开发者 https://www.devze.com 2023-03-03 12:35 出处:网络
I have a string in python, which开发者_如何学Python is in this format: [NUMBER][OPERATOR][NUMBER][UNNEEDED JUNK]

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

精彩评论

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