Sam Weisenthal
Sam Weisenthal

Reputation: 2951

Remove characters between backslashes in string in Python

Is this the correct way to remove everything between two backward slashes?

clean = re.sub(r'\\.+?\\', '', clean)

Example input:

a\ue00f\ue010\ue011\ue012\ue013\a

Example output:

aa

Upvotes: 0

Views: 894

Answers (1)

dylrei
dylrei

Reputation: 1746

Maybe split() can help here:

>>> input = r'a\ue00f\ue010\ue011\ue012\ue013\a'
>>> elems = input.split('\\')
>>> ''.join((elems[0], elems[-1]))
'aa'

Upvotes: 1

Related Questions