nadermx nadermx - 9 months ago 35
Python Question

How to remove text between variables

I currently have a string that has variables in it.

I'm trying to delete

&thingy=(all text that is in here)

The order might not always be that, and the text after the
will change.

I started doing something like this, but I feel there has to be quicker alternative:

cleanlist = []
variables = url.split('&')
for t in variables:
if not t.split('=', 1)[0] == 'thingy':
cleanlist.append(t.split('=', 1)[0])


You can substitute using regex.

import re
p = re.compile(ur'(&thingy=.*)&')
test_str = u""
subst = u"&"

result = re.sub(p, subst, test_str)

>>> result