Python仅找到具有特定长度数字的字符串

Jes*_*aal 5 python numbers

我正在尝试创建一个脚本,该脚本从output.txt搜索仅具有特定长度数字的数字字符串。

示例output.txt:

12345678
77777
12123887
Run Code Online (Sandbox Code Playgroud)

当我使用时:

import re 
f = open('output.txt', 'r')
strings = re.findall(r'(\d{5,5})', f.read())
print strings
Run Code Online (Sandbox Code Playgroud)

我只想输出:77777而不是:

12345
77777
12123
Run Code Online (Sandbox Code Playgroud)

fie*_*vel 1

只需指定您期望的边界:您期望 5 位数字不被其他数字包围:

with open("output.txt", "r") as f:
   strings=re.findall(r'\b(\d{5})\b', f.read())
   print strings
Run Code Online (Sandbox Code Playgroud)