Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想读整行。
例子:
TempFile = open('file.tmp', 'r') for line in TempFile: m = re.findall("(?:\d{1,3}\.){3}\d{1,3}", line) for x in m: print <The whole line, but how?>
整行仍然保存在line循环中的变量中,尽管我不确定为什么要为找到的正则表达式的每个匹配项打印整行。
line
TempFile = open('file.tmp', 'r') for line in TempFile: m = re.findall(r"(?:\d{1,3}\.){3}\d{1,3}", line) for x in m: print line
请注意,我还将您的正则表达式字符串更改为原始字符串文字,以确保反斜杠被正确转义,在这里不会有什么不同,但如果您尝试将单词边界与 匹配,它会有所不同\b,例如。
\b