繁体   English   中英

re.sub删除字符串后的8个字符

[英]re.sub delete 8 characters after string

我已经搜索了一段时间,但我搜索的内容没有任何运气。 简单地说,我有一个长字符串,在这种情况下,我想从中删除唯一标识符JF32-000001007。 但是我似乎找不到使用re.sub的正确方法。

我认为也许我可以做一些事情来影响:* =示例中的通配符查找:J ****-*********并什么也不替换。 我之所以保留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并说实话,它使我烦恼不已。.因此,我对成为菜鸟感到抱歉,但如果我能得到一个可行的例子,那么这些部分将融为一体。

亲切的问候,Sym。

怎么样

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

匹配“ J”,后跟四个字符,后跟“-”,后跟9个字符。

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM