import re
regex = re.compile(
r"""(?x)
(?<= # Assert that the text before the current location is:
\b # word boundary
int # "int"
\s # whitespace
) # End of lookbehind
[^=]* # Match any number of characters except =
(?<!\s) # Assert that the previous character isn't whitespace.
(?= # Assert that the following text is:
\s* # optional whitespace
= # "="
) # end of lookahead""")
with open(filename) as fn:
text = fn.read()
matches = regex.findall(text)
如果文件可以轻松放入内存中,则可以通过一个regex调用获得:
如果
^{pr2}$int
和=
之间只能有一个单词,那么regex就更简单了:我会在整个文本中使用正则表达式(您也可以在一行上这样做)。这将打印“int”和“=”之间的字符串
相关问题 更多 >
编程相关推荐