Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate words in a string using regex

Tags:

python

regex

I'm working on my regex skills and i find one of my strings having duplicate words at the starting. I would like to remove the duplicate and just have one word of it -

server_server_dev1_check_1233.zzz
server_server_qa1_run_1233.xyz
server_server_dev2_1233.qqa
server_dev1_1233.zzz
data_data_dev9_check_660.log

I used the below regex but i get both server_server in my output,

((.*?))_(?!\D)

How can i have my output just to one server_ if there are two or more and if its only one server_, then take as is? The output doesn't have to contain the digits and also the part after . i.e. .zzz, .xyz etc

Expected output -

server_dev1_check
server_qa1_run
server_dev2
server_dev1
data_dev9_check
like image 598
sdgd Avatar asked Sep 17 '26 02:09

sdgd


1 Answers

you could back reference the word in your search expression:

>>> s = "server_server_dev1_check_1233.zzz"
>>> re.sub(r"(.*_)\1",r"\1",s)
'server_dev1_check_1233.zzz'

and use the "many times" suffix so if there are more than 2 occurrences it still works:

'server_server_server_dev1_check_1233.zzz'
>>> re.sub(r"(.*_)\1{1,}",r"\1",s)
'server_dev1_check_1233.zzz'

getting rid of the suffix is not the hardest part, just capture the rest and discard the end:

>>> re.sub(r"(.*_)\1{1,}(.*)(_\d+\..*)",r"\1\2",s)
'server_dev1_check'
like image 175
Jean-François Fabre Avatar answered Sep 18 '26 15:09

Jean-François Fabre