Skip to main content
 首页 » 编程设计

Python正则表达式获取长路径中的文件名

2025年05月04日25开发

我需要分析一些日志文件,如下所示,我想检索 3 部分数据,

  1. 时间
  2. 目录的一部分,在这种情况下,它将是输入文件中的 ABC 和 DEF。
  3. 输入文件中的文件名,即本例中的 2C.013000000B.dat、20100722B.TXT、20100722D1-XYZ.TXT 和 2C.250B。

我用了这个正则表达式,但是获取不到第三部分。

(\d\d:\d\d:\d\d).*(ABC|DEF).*\\(\d\w\.?\w\..*)\soutput.* 

任何建议将不胜感激。

08:38:36   TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-ABC\2C.013000000B.dat output file=c:\local\project1\data\2C.013000000B.dat.ext 
06:40:37   TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-ABC\20100722B.TXT output file=c:\local\project1\data\20100722B.TXT.ext 
06:40:39   TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-DEF\20100722D1-XYZ.TXT output file=c:\local\project1\data\20100722D1-YFP.TXT.ext 
06:40:42   TestModule - [INFO]result success !! ftp_site=ftp.test.com file_dir=CPY input file=\root\level1\level2-DEF\2C.250B output file=c:\local\project1\data\2C.250B.ext 

BR

爱德华

请您参考如下方法:

正则表达式非常擅长解决此类问题 - 即解析日志文件记录。 MarcoS 的回答很好地解决了您眼前的问题。然而,另一种方法是编写一个(可重用的)通用函数,它将日志文件记录分解为其各种组件,并返回包含所有这些已解析组件的匹配对象。分解后,可以轻松地将测试应用于组件部分以检查各种要求(例如输入文件路径必须以 ABC 或 DEF 结尾)。这是一个 python 脚本,它具有这样一个功能:decomposeLogEntry() 并演示了如何使用它来解决您手头的问题:

import re 
def decomposeLogEntry(text): 
    r""" Decompose log file entry into its various components. 
 
    If text is a valid log entry, return regex match object of 
    log entry components strings. Otherwise return None.""" 
    return re.match(r""" 
        # Decompose log file entry into its various components. 
        ^                            # Anchor to start of string 
        (?P<time>\d\d:\d\d:\d\d)     # Capture: time 
        \s+ 
        (?P<modname>\w+?)            # Capture module name 
        \s-\s\[ 
        (?P<msgtype>[^]]+)           # Capture message type 
        \] 
        (?P<message>[^!]+)           # Capture message text 
        !!\sftp_site= 
        (?P<ftpsite>\S+?)            # Capture ftp URL 
        \sfile_dir= 
        (?P<filedir>\S+?)            # Capture file directory? 
        \sinput\sfile= 
        (?P<infile>                  # Capture input path and filename 
          (?P<infilepath>\S+)\\      # Capture input file path 
          (?P<infilename>[^\s\\]+)   # Capture input file filename 
        ) 
        \soutput\sfile= 
        (?P<outfile>                 # Capture input path and filename 
          (?P<outfilepath>\S+)\\     # Capture output file path 
          (?P<outfilename>[^\s\\]+)  # Capture output file filename 
        ) 
        \s*                          # Optional whitespace at end. 
        $                            # Anchor to end of string 
        """, text, re.IGNORECASE | re.VERBOSE) 
 
# Demonstrate decomposeLogEntry function. Print components of all log entries. 
f=open("testdata.log") 
mcnt = 0 
for line in f: 
    # Decompose this line into its components. 
    m = decomposeLogEntry(line) 
    if m: 
        mcnt += 1 
        print "Match number %d" % (mcnt) 
        print "  Time:             %s" % m.group("time") 
        print "  Module name:      %s" % m.group("modname") 
        print "  Message type:     %s" % m.group("time") 
        print "  Message:          %s" % m.group("message") 
        print "  FTP site URL:     %s" % m.group("ftpsite") 
        print "  Input file:       %s" % m.group("infile") 
        print "  Input file path:  %s" % m.group("infilepath") 
        print "  Input file name:  %s" % m.group("infilename") 
        print "  Output file:      %s" % m.group("outfile") 
        print "  Output file path: %s" % m.group("outfilepath") 
        print "  Output file name: %s" % m.group("outfilename") 
        print "\n", 
f.close() 
 
# Next pick out only the desired data. 
f=open("testdata.log") 
mcnt = 0 
matches = [] 
for line in f: 
    # Decompose this line into its components. 
    m = decomposeLogEntry(line) 
    if m: 
        # See if this record meets desired requirements 
        if re.search(r"ABC$|DEF$", m.group("infilepath")): 
            matches.append(line) 
f.close() 
print "There were %d matching records" % len(matches) 

此功能不仅可以挑选出您感兴趣的各个部分,还可以验证输入并拒绝格式错误的记录。一旦编写和调试,该函数可以被其他需要分析日志文件以满足其他需求的程序重用。

以下是应用于测试数据时脚本的输出:

r""" 
Match number 1 
  Time:             08:38:36 
  Module name:      TestModule 
  Message type:     08:38:36 
  Message:          result success 
  FTP site URL:     ftp.test.com 
  Input file:       \root\level1\level2-ABC\2C.013000000B.dat 
  Input file path:  \root\level1\level2-ABC 
  Input file name:  2C.013000000B.dat 
  Output file:      c:\local\project1\data\2C.013000000B.dat.ext 
  Output file path: c:\local\project1\data 
  Output file name: 2C.013000000B.dat.ext 
 
Match number 2 
  Time:             06:40:37 
  Module name:      TestModule 
  Message type:     06:40:37 
  Message:          result success 
  FTP site URL:     ftp.test.com 
  Input file:       \root\level1\level2-ABC\20100722B.TXT 
  Input file path:  \root\level1\level2-ABC 
  Input file name:  20100722B.TXT 
  Output file:      c:\local\project1\data\20100722B.TXT.ext 
  Output file path: c:\local\project1\data 
  Output file name: 20100722B.TXT.ext 
 
Match number 3 
  Time:             06:40:39 
  Module name:      TestModule 
  Message type:     06:40:39 
  Message:          result success 
  FTP site URL:     ftp.test.com 
  Input file:       \root\level1\level2-DEF\20100722D1-XYZ.TXT 
  Input file path:  \root\level1\level2-DEF 
  Input file name:  20100722D1-XYZ.TXT 
  Output file:      c:\local\project1\data\20100722D1-YFP.TXT.ext 
  Output file path: c:\local\project1\data 
  Output file name: 20100722D1-YFP.TXT.ext 
 
Match number 4 
  Time:             06:40:42 
  Module name:      TestModule 
  Message type:     06:40:42 
  Message:          result success 
  FTP site URL:     ftp.test.com 
  Input file:       \root\level1\level2-DEF\2C.250B 
  Input file path:  \root\level1\level2-DEF 
  Input file name:  2C.250B 
  Output file:      c:\local\project1\data\2C.250B.ext 
  Output file path: c:\local\project1\data 
  Output file name: 2C.250B.ext 
 
There were 4 matching records 
"""