Better way / regex to extract values form a dictionary

Ganesh Pal ganesh1pal at
Sat Jul 21 07:37:04 EDT 2018

I have one of the dictionary values in the below format


I need to extract the file name in the path example, say test04_Failure.log
and testcase no i.e test04

Here is my solutions:

gpal-cwerzvd-1# vi
import re

Common_dict = {}
Common_dict['filename'] =

def return_filename_test_case(filepath):
    if filepath:
       filename = re.findall(r'(test\d{1,4}_\S+)', filepath)
    if filename:
       testcase = re.findall(r'(test\d{1,4})', ''.join(filename))

    return filename, testcase

if Common_dict['filename']:
   path = Common_dict['filename']
   fname, testcase = return_filename_test_case(path)
   print fname, testcase

qerzvd-1# python

Please suggest how can this code can be optimized  further looks messy ,
what would be your one liner or a simple solution to return both test-case
no and filename

I am on Python 2.7 and Linux


More information about the Python-list mailing list