Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've got a string looks like this

ABC(a =2,b=3,c=5,d=5,e=Something)

I want the result to be like

ABC(a =2,b=3,c=5)

What's the best way to do this? I prefer to use regular expression in Python.

Sorry, something changed, the raw string changed to

ABC(a =2,b=3,c=5,dddd=5,eeee=Something)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
296 views
Welcome To Ask or Share your Answers For Others

1 Answer

longer = "ABC(a =2,b=3,c=5,d=5,e=Something)"

shorter = re.sub(r',s*d=d+,s*e=[^)]+', '', longer)

# shorter: 'ABC(a =2,b=3,c=5)'

When the OP finally knows how many elements are there in the list, he can also use:

shorter = re.sub(r',s*d=[^)]+', '', longer)

it cuts the , d= and everything after it, but not the right parenthesis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...