Skip to content
Advertisement

Replacing character in string doesn’t do anything

I have a list like this,

JavaScript

Expected output:

JavaScript

Removing the brackets is easy by using .replace(), but I don’t want to remove the brackets from strings (#) and (##).

my code:

JavaScript

but this doesn’t remove the brackets from the words.

Advertisement

Answer

You can use re.sub. In particular, note that it can take a function as repl parameter. The function takes a match object, and returns the desired replacement based on the information the match object has (e.g., m.group(1)).

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement