lalithkumar
lalithkumar

Reputation: 3540

Replace string which has dynamic character in python

Trying to replace the string with regular expression and could not success.

The strings are "LIVE_CUS2_PHLR182" ,"LIVE_CUS2ee_PHLR182" and "PHLR182 - testing recovery".Here I need to get PHLR182 as an output with all the string but where second string has "ee" which is not constant. It can be string or number with 2 character.Below is the code what I have tried.

For first and last string I just simply used replace function like below.

s = "LIVE_CUS2_PHLR182"
s.replace("LIVE_CUS2_", ""), s.replace(" - testing recovery","")
>>> PHLR182

But for second I tried like below.

1. s= "LIVE_CUS2ee_PHLR182"
   s.replace(r'LIVE_CUS2(\w+)*_','')

2. batRegex = re.compile(r'LIVE_CUS2(\w+)*_PHLR182')
   mo2 = batRegex.search('LIVE_CUS2dd_PHLR182')
   mo2.group()

3. re.sub(r'LIVE_CUS2(?is)/s+_PHLR182', '', r)

In all case I could not get "PHLR182" as an output. Please help me.

Upvotes: 0

Views: 535

Answers (1)

r.ook
r.ook

Reputation: 13858

I think this is what you need:

import re

texts = """LIVE_CUS2_PHLR182
LIVE_CUS2ee_PHLR182
PHLR182 - testing recovery""".split('\n')

pat = re.compile(r'(LIVE_CUS2\w{,2}_| - testing recovery)')
#                   1st alt pattern | 2nd alt pattern
#                   Look for 'LIV_CUS2_' with up to two alphanumeric characters after 2
#                               ... or Look for ' - testing recovery'

results = [pat.sub('', text) for text in texts]
# replace the matched pattern with empty string

print(f'Original: {texts}')
print(f'Results: {results}')

Result:

Original: ['LIVE_CUS2_PHLR182', 'LIVE_CUS2ee_PHLR182', 'PHLR182 - testing recovery']
Results: ['PHLR182', 'PHLR182', 'PHLR182']

Python Demo: https://repl.it/repls/ViolentThirdAutomaticvectorization

Regex Demo: https://regex101.com/r/JiEVqn/2

Upvotes: 1

Related Questions