[Solved] Replace [1-2] in to 1 in Python


You could use a regex like:

^(.*)\[([0-9]+).*?\](.*)$

and replace it with:

$1$2$3

Here’s what the regex does:

^ matches the beginning of the string
(.*) matches any character any amount of times, and is also the first capture group
\[ matches the character [ literally
([0-9]+) matches any number 1 or more times, and is also the second capture group
.*? matches any character any amount of times, but tries to find the smallest match
\] matches the character ] literally
(.*) matches any characters any amount of times
$ matches the end of the string

By replacing it with $1$2$3, you are replacing it with the text in the first capture group, followed by the text in the second capture group, followed by the text in the third capture group.

Here’s a live preview on regex101.com

2

solved Replace [1-2] in to 1 in Python