回复sub删除字符串后的8个字符

2024-09-30 03:24:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经四处寻找了一段时间,在我寻找的东西上没有任何运气。简单地说,我有一个长字符串,我想删除它的唯一标识符,在这个例子中是JF32-000001007。然而,我似乎找不到正确的方法来做它使用回复sub是的

我想也许我可以做点什么来影响: *=示例中的通配符 查找:J*****-********并替换为nothing。我之所以把J和-放在那里是因为它们是一致的。其他一切都变了。到目前为止,我得出的结论如下:

import re
string = "E/Exception ['thisdevice:JFW32-000001007.IdName = telephonepole (null) has no hope, magic will be discard']"

final = re.sub(r'J.*?-', '', string)
print final

这只是替换上半部分,而不是数字

import re
string = "E/Exception ['thisdevice:JFW32-000001007.IdName = telephonepole (null) has no hope, magic will be discard']"

final = re.sub(r'E.*?{14}', '', string)
print final

这会产生一个异常,表示它不正确(我认为我没有正确地使用它)

我试着读了https://docs.python.org/2/library/re.html#re.sub,说实话,它把我搞糊涂了。。所以我很抱歉成为一个笨蛋,但也许如果我得到一个工作的例子,在我的情况下,碎片会一起。你知道吗

谨致问候, 符号。你知道吗


Tags: noimportrestringmagicexceptionnull例子
2条回答
import re
string = "E/Exception ['thisdevice:JFW32-000001007.IdName = telephonepole (null) has no hope, magic will be discard']"

final = re.sub(r'J[\w\d]+\-[\d]+', '', string)
print final
>>>E/Exception ['thisdevice:.IdName = telephonepole (null) has no hope, magic will be discard']

怎么样

re.sub(r'J.{4}-.{9}', '', string)

匹配“J”,后跟4个字符,后跟“-”,后跟9个字符。你知道吗

相关问题 更多 >

    热门问题