Reputation: 3434
I have a string made of mixed mp3 information that I must try to match against a pattern made of arbitrary strings and tokens. It works like that :
the Beatles_Abbey_Road-SomeWord-1969
the %Artist_%Album-SomeWord-%Year
2 possible matches found :
[1] {'Artist': 'Beatles', 'Album':'Abbey_Road', 'Year':1969}
[2] {'Artist': 'Beatles_Abbey', 'Album':'Road', 'Year':1969}
As an example, let say pattern is artist name followed by title (delimiter: '-').
Example 1:
>>> artist = 'Bob Marley'
>>> title = 'Concrete Jungle'
>>> re.findall(r'(.+)-(.+)', '%s-%s' % (artist,title))
[('Bob Marley', 'Concrete Jungle')]
So far, so good. But...
I have no control over the delimiter used and have no guarantee that it's not present in the tags, so trickier cases exist :
Example 2:
>>> artist = 'Bob-Marley'
>>> title = 'Roots-Rock-Reggae'
>>> re.findall(r'(.+)-(.+)', '%s-%s' % (artist,title))
[('Bob-Marley-Roots-Rock', 'Reggae')]
As expected, it doesn't work in that case.
How can I generate all possible combinations of artist/title ?
[('Bob', 'Marley-Roots-Rock-Reggae'),
('Bob-Marley', 'Roots-Rock-Reggae')
('Bob-Marley-Roots', 'Rock-Reggae'),
('Bob-Marley-Roots-Rock', 'Reggae')]
Are regex the tool to use for that job ?
Please keep in mind that number of tags to match and delimiters between those tags are not fixed but user defined (so the regex to use has to be buildable dynamically).
I tried to experiment with greedy vs minimal matching and lookahead assertions with no success.
Thanks for your help
Upvotes: 3
Views: 811
Reputation: 208545
This solution seems to work. In addition to the regex you will need a list of tuples to describe the pattern, where each element corresponds to one capturing group of the regex.
For your Beatles example, it would look like this:
pattern = r"the (.+_.+)-SomeWord-(.+)"
groups = [(("Artist", "Album"), "_"), ("Year", None)]
Because the Artist
and Album
are only split by a single separator, they will be captured together in one group. The first item in the list indicates that the first capture group will be split into and Artist
and an Album
, and will use _
as the separator. The second item in the list indicates that the second capture group will be used as the Year
directly, since the second element in the tuple is None
. You could then call the function like this:
>>> get_mp3_info(groups, pattern, "the Beatles_Abbey_Road-SomeWord-1969")
[{'Album': 'Abbey_Road', 'Year': '1969', 'Artist': 'Beatles'}, {'Album': 'Road', 'Year': '1969', 'Artist': 'Beatles_Abbey'}]
Here is the code:
import re
from itertools import combinations
def get_mp3_info(groups, pattern, title):
match = re.match(pattern, title)
if not match:
return []
result = [{}]
for i, v in enumerate(groups):
if v[1] is None:
for r in result:
r[v[0]] = match.group(i+1)
else:
splits = match.group(i+1).split(v[1])
before = [d.copy() for d in result]
for comb in combinations(range(1, len(splits)), len(v[0])-1):
temp = [d.copy() for d in before]
comb = (None,) + comb + (None,)
for j, split in enumerate(zip(comb, comb[1:])):
for t in temp:
t[v[0][j]] = v[1].join(splits[split[0]:split[1]])
if v[0][0] in result[0]:
result.extend(temp)
else:
result = temp
return result
And another example with Bob Marley:
>>> pprint.pprint(get_mp3_info([(("Artist", "Title"), "-")],
... r"(.+-.+)", "Bob-Marley-Roots-Rock-Reggae"))
[{'Artist': 'Bob', 'Title': 'Marley-Roots-Rock-Reggae'},
{'Artist': 'Bob-Marley', 'Title': 'Roots-Rock-Reggae'},
{'Artist': 'Bob-Marley-Roots', 'Title': 'Rock-Reggae'},
{'Artist': 'Bob-Marley-Roots-Rock', 'Title': 'Reggae'}]
Upvotes: 1
Reputation: 50557
What about something like this instead of using a regular expression?
import re
string = "Bob-Marley-Roots-Rock-Reggae"
def allSplits(string, sep):
results = []
chunks = string.split('-')
for i in xrange(len(chunks)-1):
results.append((
sep.join(chunks[0:i+1]),
sep.join(chunks[i+1:len(chunks)])
))
return results
print allSplits(string, '-')
[('Bob', 'Marley-Roots-Rock-Reggae'), ('Bob-Marley', 'Roots-Rock-Reggae'), ('Bob-Marley-Roots', 'Rock-Reggae'), ('Bob-Marley-Roots-Rock', 'Reggae')]
Upvotes: 0