簡體   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