Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:42+00:00 2026-05-23T19:31:42+00:00

The context I have a string made of mixed mp3 information that I must

  • 0

The context

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 :

  1. The program shows the user a given string

the Beatles_Abbey_Road-SomeWord-1969

  1. User enter a pattern to help program parse the string

the %Artist_%Album-SomeWord-%Year

  1. Then I’d like to show results of the matches (but need your help for that)

2 possible matches found :
[1] {‘Artist’: ‘Beatles’, ‘Album’:’Abbey_Road’, ‘Year’:1969}
[2] {‘Artist’: ‘Beatles_Abbey’, ‘Album’:’Road’, ‘Year’:1969}

The problem

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:31:43+00:00Added an answer on May 23, 2026 at 7:31 pm

    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'}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a program that gets some strings in input from the user
I have a content string that starts with an unordered list I want to
I have the following code inside a method: string username = (string)context[UserName]; string un
I have a home made logging system in my app. Of the things that
I have to establish an HttpListener that will wait for requests made by our
I have made a context menu but I cant get the itemListener to work
I have made an app that has 3 activities. In the fisrt activity(Import) i
With the help of this tutorial i have made an app that has 3
I have a String with this content: href=http://www.website.com/ /> [...] [...] I just want
I have a string of HTML contained in a var called content. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.