假设testFile文件中有以下行:
Test Line in File
Test Line in File
Test Line in File
Test Line in File Line
Test Line in File Line
有没有可能做一个关于芬德尔()这将允许我在每行中找到一个“pattern”实例?例如,如果我做了len(关于芬德尔(“行”,testfile,0)),程序将返回7。我要它返回5。我正在考虑类似“Line.*.*\n”的内容,但它仍将返回7。为了澄清,我想避免使用:
count = 0
with open(testFile, "r") as file:
for line in file:
re.match(pattern, testFile, 0)
#etc
感谢您的帮助。你知道吗
将整个文件加载到内存中来执行
re.findall
是没有意义的,这样做会失去在找到第一个匹配项时短路的能力对于这样一个简单的匹配,使用这个更有效。。。你知道吗
…它使用highly optimized searching algorithm,比使用正则表达式快得多(上次我检查时快了8倍左右)。你知道吗
你可以使用Multiline标志!你知道吗
但是,在这种情况下,我不鼓励使用regex!你知道吗
相关问题 更多 >
编程相关推荐