替换在python中具有动态字符的字符串

时间:2019-11-21 14:52:53

标签: python regex replace

尝试用正则表达式替换字符串,但操作失败。

字符串是“ LIVE_CUS2_PHLR182”,“ LIVE_CUS2ee_PHLR182”和“ PHLR182-测试恢复”。在这里,我需要获取PHLR182作为所有字符串的输出,但第二个字符串的“ ee”不是恒定的。它可以是2个字符的字符串或数字。下面是我尝试过的代码。

对于第一个和最后一个字符串,我只使用了replace函数,如下所示。

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

但是我第二次尝试如下。

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)

在所有情况下,我都无法获得“ PHLR182”作为输出。请帮助我。

1 个答案:

答案 0 :(得分:1)

我认为这是您所需要的:

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}')

结果:

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

Python演示:https://repl.it/repls/ViolentThirdAutomaticvectorization

正则表达式演示:https://regex101.com/r/JiEVqn/2