Python: Use Regular expression to remove something

2024/7/7 8:22:09

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)
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.

https://en.xdnf.cn/q/120297.html

Related Q&A

Python delete row in file after reading it

I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i dont waste to much o…

Trying to keep the same type after saving a dataframe in a csv file

When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?

Merge blocks of images to produce new image

Hi is there a way of merging specific blocks from multiple images of same size(say 100x100) and putting them together in a new image. To be more specific, consider I have a set of images which have bee…

Removing Characters from python Output

I did alot of work to remove the characters from the spark python output like u u u" [()/" which are creating problem for me to do the further work. So please put a focus on the same .I have …

How to make a tkinter entry default value permanent

I am writing a program in python that will take in specific formats, a Phone number and dollar/cent values. How can I make tkinter have default value which is permanent, not deletable. For example (XXX…

distribute value in buckets

Consider below DF, I have an input number=4 to be inserted evenly in different hour buckets.p_hourly mins 0 2020-09-10 07:00:00 60.0 1 2020-09-10 08:00:00 60.0 2 2020-09-10 09:00:00 60…

for loop over list break and continue

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entr…

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…

How can I filter the domains served by a CDN from a list of domain names?

I have a list of domains and I need to filter the domains served by a CDN(Content Delivery Network). I am going to use python script to do that. At the first I was thinking I can identify them from the…

Convert int(round(time.time())) to C# [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…