Entity
Entity

Reputation: 8222

Python string trimming

I have a string in python, which 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.

Upvotes: 1

Views: 1799

Answers (2)

Kabie
Kabie

Reputation: 10663

re.search(r'\d+.\d+','123+55.[)]1').group()

This should work.

Upvotes: 3

orlp
orlp

Reputation: 118016

This is a simple regular expression:

import re

s = "5+5.[)]1"
s = re.search("\d+\+\d+", s).group()
print(s) # 5+5

Upvotes: 6

Related Questions