String template to regex in python

Question:

I am trying to make a parser of file paths that follow an arbitrary naming convention.

I am wanting to take an arbitrary string.Template and get the re.Match.groupdict() corresponding to a string that follows the string.Template. I would also like to specify the delimiter of the string template such as $Name1-$Name2 or $Name1_$Name2 both returning {‘Name1’: ‘foo’, ‘Name2’: ‘bar’} for the respective inputs.

It would also be nice to be able to have a syntax for specifying a non-capture group such as $[ignore]_$keep resulting in {‘keep’: ‘foo’}.

Asked By: georgwalker45

||

Answers:

Found a package called lucidity that does exactly what I need.

Answered By: georgwalker45

Here is an example of a function that implements the functionality you described:

import re

def parse_file_path(path, template, delimiter="_"):
    pattern = template.replace("$", "$").replace("[", "[").replace("]", "]").replace(".", ".")
    pattern = pattern.replace(delimiter, ".*")
    pattern = re.sub(r"$w+", "(?P<\0>[^" + delimiter + "]+)", pattern)
    match = re.match(pattern, path)
    if match:
        return {k: v for k, v in match.groupdict().items() if not k.startswith("_")}
    else:
        return None

You can use this function as follows:

path = "foo_bar"
template = "$Name1_$Name2"
result = parse_file_path(path, template)
print(result) # Output: {'Name1': 'foo', 'Name2': 'bar'}

path = "foo-bar"
template = "$Name1-$Name2"
result = parse_file_path(path, template, delimiter="-")
print(result) # Output: {'Name1': 'foo', 'Name2': 'bar'}

path = "foo_bar"
template = "$[ignore]_$keep"
result = parse_file_path(path, template)
print(result) # Output: {'keep': 'bar'}

Answered By: Roman Yakimov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.