Home > database >  Regex : replace url inside string
Regex : replace url inside string

Time:11-29

i have

string = 'Server:xxx-zzzzzzzzz.eeeeeeeeeee.frPIPELININGSIZE'

i need a python regex expression to identify xxx-zzzzzzzzz.eeeeeeeeeee.fr to do a sub-string function to it

Expected output :

string : 'Server:PIPELININGSIZE'

the URL is inside a string, i tried a lot of regex expressions

CodePudding user response:

Not sure if this helps, because your question was quite vaguely formulated. :)

import re

string = 'Server:xxx-zzzzzzzzz.eeeeeeeeeee.frPIPELININGSIZE'

string_1 = re.search('[a-z.-] ([A-Z] )', string).group(1)

print(f'string: Server:{string_1}')

Output:

string: Server:PIPELININGSIZE

CodePudding user response:

No regex. single line use just to split on your target word.

string = 'Server:xxx-zzzzzzzzz.eeeeeeeeeee.frPIPELININGSIZE'

last = string.split("fr",1)[1]

first =string[:string.index(":")]
print(f'{first} : {last}')

Gives #

Server:PIPELININGSIZE
  • Related