Improve regular expressions

- Remove redundant escape
- Remove erroneous character
- Remove duplicate character
This commit is contained in:
JoeLametta
2019-01-16 19:57:32 +00:00
parent 3e79032b63
commit 8dfcc5b5ec
2 changed files with 5 additions and 5 deletions

View File

@@ -285,9 +285,9 @@ def validate_template(template, kind):
Raise exception if disc/track template includes invalid variables
"""
if kind == 'disc':
matches = re.findall(r'%[^A,R,S,X,d,r,x,y]', template)
matches = re.findall(r'%[^ARSXdrxy]', template)
elif kind == 'track':
matches = re.findall(r'%[^A,R,S,X,a,d,n,r,s,t,x,y]', template)
matches = re.findall(r'%[^ARSXadnrstxy]', template)
if '%' in template and matches:
raise ValueError(kind + ' template string contains invalid '
'variable(s): {}'.format(', '.join(matches)))

View File

@@ -45,7 +45,7 @@ class PathFilter(object):
def separators(path):
# replace separators with a space-hyphen or hyphen
path = re.sub(r'[:]', ' -', path, re.UNICODE)
path = re.sub(r'[\|]', '-', path, re.UNICODE)
path = re.sub(r'[|]', '-', path, re.UNICODE)
return path
# change all fancy single/double quotes to normal quotes
@@ -56,12 +56,12 @@ class PathFilter(object):
if self._special:
path = separators(path)
path = re.sub(r'[\*\?&!\'\"\$\(\)`{}\[\]<>]',
path = re.sub(r'[*?&!\'\"$()`{}\[\]<>]',
'_', path, re.UNICODE)
if self._fat:
path = separators(path)
# : and | already gone, but leave them here for reference
path = re.sub(r'[:\*\?"<>|"]', '_', path, re.UNICODE)
path = re.sub(r'[:*?"<>|]', '_', path, re.UNICODE)
return path