Regex can be a real pain to get your head around and/or parse visually.

I’m going to use this post to gather regex patterns/expressions that may prove useful for those of you in visual effects.

# This expression breaks a path down in to 4 named groups; the path, the file basename, frame-padding and extension

^(?P<PATH>.*\\)(?P<BASENAME>[^\.]*)(?:\.)?(?P<PADDING>.*)\.(?P<EXT>.*)

# Here’s how you might roll this out in your python script. Here I’m populating a dictionary with the path components.

pathparts = {}
regex_expr = r'^(?P<PATH>.*\\)(?P<BASENAME>[^\.]*)(?:\.)?(?P<PADDING>.*)\.(?P<EXT>.*)'
regex_groups = re.search(regex_expr, path) # .group('EXT')
pathparts['dir'] = regex_groups.group('PATH')
pathparts['filename'] = regex_groups.group('BASENAME')
pathparts['padding'] = regex_groups.group('PADDING')
pathparts['extension'] = regex_groups.group('EXT')

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Skip to toolbar