简体   繁体   中英

Regex : replace url inside string

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

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

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

The wording of the question suggests that you wish to find the hostname in the string, but the expected output suggests that you want to remove it. The following regular expression will create a tuple and allow you to do either.

import re

str = "Server:xxx-zzzzzzzzz.eeeeeeeeeee.frPIPELININGSIZE"

p = re.compile('^([A-Za-z]+[:])(.*?)([A-Z]+)$')
m = re.search(p, str)
result = m.groups()
# ('Server:', 'xxx-zzzzzzzzz.eeeeeeeeeee.fr', 'PIPELININGSIZE')

Remove the hostname:

print(f'{result[0]} {result[2]}')
# Output: 'Server: PIPELININGSIZE'

Extract the hostname:

print(result[1])
# Output: 'xxx-zzzzzzzzz.eeeeeeeeeee.fr'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM