试图用正则表达式替换字符串,但未能成功。你知道吗
字符串是“LIVE\u CUS2\u PHLR182”、“LIVE\u CUS2ee\u phl182”和“phl182-测试恢复”。这里我需要将phl182作为所有字符串的输出,但第二个字符串的“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”作为输出。请帮帮我。你知道吗
我想这就是你需要的:
结果:
Python演示:https://repl.it/repls/ViolentThirdAutomaticvectorization
正则表达式演示:https://regex101.com/r/JiEVqn/2
相关问题 更多 >
编程相关推荐