HTF
HTF

Reputation: 7280

Match a string and replace remaning characters

I would like to extract the server type from the hostname and replace the remaining characters with underscores so I can then use it with the LIKE pattern match in SQLite

My initial approach was something like this (this is the excepted output):

>>> host = 'webus01'
>>> location = 'us'
>>> parts = list(host.partition(location))
>>> parts
['web', 'us', '01']
>>> parts[1] = "_" * len(parts[1])
>>> parts[2] = "_" * len(parts[2])
>>> "".join(parts) + ".%"
'web____.%'

But this won't work if the hostname starts with or contains the location name:

>>> host = 'utilityit01'
>>> pod = 'it'
>>> parts = list(host.partition(location))
>>> parts
['utilityit01', '', '']
>>> parts[1] = "_" * len(parts[1])
>>> parts[2] = "_" * len(parts[2])
>>> "".join(parts) + ".%"
'utilityit01.%'

Then I though it will be better to use RegEx for this to match only the location before digits. The re.sub function seems to be a good candidate for this task but I'm not sure how to replace all characters instead of the match group as a whole:

>>> import re
>>> re.sub(r'it\d+.*', '_', 'utilityit01a')
'utility_'

The output in this case should be: utility_____.%.

Upvotes: 1

Views: 57

Answers (2)

Binh
Binh

Reputation: 1173

You can find the substring that you want to replace first, and then replace it based on its length:

>>> import re
>>> host = 'utilityit01'
>>> substr = re.findall(r'it\d+.*', host)
>>> substr
['it01a']
>>> host.replace(substr[0], len(substr)*'_')
'utility_____'

Upvotes: 1

Rakesh
Rakesh

Reputation: 82785

Looks like you need.

import re
print(re.sub(r'(it\d+.*)', lambda x: '_'*len(x.group(1))+".%" , 'utilityit01a'))
print(re.sub(r'(us\d+.*)', lambda x: '_'*len(x.group(1))+".%" , 'webus01'))

Output:

utility_____.%
web____.%

Upvotes: 1

Related Questions