我試圖做一個正則表達式中的巨蟒從.vcf文件檢測7位數字和更新聯繫人。然後它將數字修改爲8位數字(僅在數字前加5).Thing是正則表達式不起作用。什麼是RegEx在Python中查找電話號碼?
我有作爲錯誤消息「EOL在掃描字符串文字」
regex=re.compile(r'^(25|29|42[1-3]|42[8-9]|44|47[1-9]|49|7[0-9]|82|85|86|871|87[5-8]|9[0-8])/I s/^/5/')
#Open file for scanning
f = open("sample.vcf")
#scan each line in file
for line in f:
#find all results corresponding to regex and store in pattern
pattern=regex.findall(line)
#isolate results
for word in pattern:
print word
count = count+1 #display number of occurences
wordprefix = '5{}'.format(word)
s=open("sample.vcf").read()
s=s.replace(word,wordprefix)
f=open("sample.vcf",'w')
print wordprefix
f.write(s)
f.close()
我懷疑我正則表達式是不正確的格式,用於檢測數字的特定模式與2位具有特定的格式,如25X-29X和5個位數,可以是數字的任何圖案。(共7位)
誰能幫我對這種情況採取正確的格式?
爲什麼正則表達式看起來像這樣。爲什麼不簡單''[0-9] {7}'找到行中的所有7位數字? –