在文本文件中查找字符串重新匹配python中的函数

2024-04-18 22:00:43 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前的代码。我找不到这个问题重新匹配你知道吗

import os
import re

folderpath = 'D:/Workspace'
typeOfFile = [".c", ".C", ".cpp", ".CPP"]

for dirname, dirnames, filenames in os.walk(folderpath):
    for filename in filenames:
        if filename.endswith(tuple(typeOfFile)):
            data= open(os.path.join(dirname, filename), "r").readlines()

            m= re.match(r'author*.[^:=]*[:=]*(.[^\n]+)', data, re.DOTALL)

            if m: 
                author = m.group(1)
            else:
                author = 'unknown'
            print "author is this case is:", author, "in file", filename

我想在文本文件中搜索字符串作者。你知道吗


Tags: 代码inimportrefordataifis
1条回答
网友
1楼 · 发布于 2024-04-18 22:00:43

阅读the docs

re.match(pattern, string, flags=0) 

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance.

改用^{}。你知道吗

相关问题 更多 >