0

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.

12
  • You need to use re.sub if you want to replace with a regex, not s.replace Commented Nov 21, 2019 at 14:55
  • why not "LIVE_CUS2ee_PHLR182".split('_')[-1] ? Commented Nov 21, 2019 at 14:56
  • @Keatinge I tried with re.sub as well Commented Nov 21, 2019 at 14:58
  • 1
    @lalithkumar, there's no sense in your patterns which contains explicit PHLR182 when it needs to be the output/result Commented Nov 21, 2019 at 15:02
  • 1
    What about just using re.search(r'[^_\s]+(?=\s|$)',s) Commented Nov 21, 2019 at 15:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.