2017-10-06 61 views
0

我是python編程的新手,如果我需要輸入整數2468作爲用戶交互模式編譯模式,請告訴我需要做什麼修改。通過交互方式定義re.compile的模式

代碼:

f = open ('C:\\Users\\anash\\Desktop\\SIP Success call.txt') 
file = f.read() 

import re 
regex1 = re.compile(r'INVITE sip:.*?**2468**.*?Content-Length: \d+',re.DOTALL) 

filtfile = regex1.findall(file) 
print (filtfile[0]) 
+0

我需要的程序看起來像: 打印( '進入最後四位數字') LastFourDigit =輸入() ************ 然後我需要將這些數字添加到編譯模式 – Anas

回答

0

在交互shell中,輸入以下內容:

>>> import re 
>>> regex1 = re.compile(r'INVITE sip:.*?**2468**.*?Content-Length: \d+',re.DOTALL) 
>>> filtfile = regex1.findall("Any input you want to enter") 
>>> print(filtfile[0]) 
+0

給我一個錯誤:IndexError:列表索引超出範圍 – Anas

+0

@Anas如果沒有匹配,它會。 –

+0

#在re.compile中提到的包含特定模式的段落('INVITE sip:。*?2468。*?Content-Length:\ d +') #2468的程序是變量,所以用戶需要定義這個四位數字 – Anas