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 6328331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:26:27+00:00 2026-05-24T17:26:27+00:00

I am struggling with nailing down a fairly complex regular expression to parse song

  • 0

I am struggling with nailing down a fairly complex regular expression to parse song titles with optional artist attribution from loosely-typed English. The user input comes from a single text field and the regex matches will be used to query a song database to get unique track IDs. I need to be able to get these matches:

  • \1 = song title
  • \2 = artist

while being fairly liberal in allowed formats.

Examples

The wold "by" should split the string into song title and artist (but only on word boundaries); as should a comma with/without trailing whitespace:

baby one more time by britney spears

baby one more time, britney spears

baby one more time,britney spears

  • \1 = baby one more time
  • \2 = britney spears

False positives like these are acceptable:

down by the bay

  • \1 = down
  • \2 = the bay

whatever people say i am, that’s what i’m not

  • \1 = whatever people say i am
  • \2 = that’s what i’m not

…assuming quotes can be used to mark a run of text as a song title explicitly:

"down by the bay"

  • \1 = down by the bay
  • \2 not matched

"whatever people say i am, that’s what i’m not" by arctic monkeys

  • \1 = whatever people say i am, that’s what i’m not
  • \2 = arctic monkeys

Single quotes should work too, but obviously not if they appear within the title:

‘whatever people say i am, that’s what i’m not’

  • \1 = whatever people say i am, that
  • \2 = s what i’m not’

Additionally, if quotes are in use, the word "by" or a comma are optional:

"down by the bay" raffi

  • \1 = down by the bay
  • \2 = raffi

However, if there are no quotes, and more than one "by", then only the last "by" should be used as a delimiter:

down by the bay by raffi

  • \1 = down by the bay
  • \2 = raffi

Is this even possible with a single regex? Or would the more sane way be to split it up into multiple expressions? Either way, what might this look like?

  • 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-24T17:26:27+00:00Added an answer on May 24, 2026 at 5:26 pm

    Here is an example, using C#:

    var regex = @"^((""(?<title>[^""]+)""|'(?<title>[^']+)')(\s*,\s*|\s+by\s+)?|(?<title>.*)(\s*,\s*|\s+by\s+))\s*(?<artist>.*)$";
    
    var items = new []{
        "baby one more time by britney spears",
        "baby one more time, britney spears",
        "baby one more time,britney spears",
        "down by the bay",
        "whatever people say i am, that's what i'm not",
        "\"down by the bay\"",
        "\"whatever people say i am, that's what i'm not\" by arctic monkeys",
        "'whatever people say i am, that's what i'm not'",
        "\"down by the bay\" raffi",
        "down by the bay by raffi",
    };
    
    foreach (var item in items)
    {
        var match = Regex.Match(item, regex, RegexOptions.ExplicitCapture);
        Console.WriteLine(match.Groups["title"] + " - " + match.Groups["artist"]);
    }
    

    Output matches your specification, as far as I can tell:

    baby one more time - britney spears
    baby one more time - britney spears
    baby one more time - britney spears
    down - the bay
    whatever people say i am - that's what i'm not
    down by the bay - 
    whatever people say i am, that's what i'm not - arctic monkeys
    whatever people say i am, that - s what i'm not'
    down by the bay - raffi
    down by the bay - raffi
    

    You can actually make it better for the single-quote case by allowing apostrophes inside words:

    var regex = @"^((""(?<title>[^""]+)""|'(?<title>([^']|(?<=\w)'(?=\w))+)')(\s*,\s*|\s+by\s+)?|(?<title>.*)(\s*,\s*|\s+by\s+))\s*(?<artist>.*)$";
    

    Which fixes this case:

    whatever people say i am, that's what i'm not - 
    

    Here’s a commented version of the regex, which explains what each part does (should be matched with RegexOptions.ExplicitCapture|RegexOptions.IgnorePatternWhitespace):

    var regex = @"
    ^
      (
        (
          ""(?<title>[^""]+)""               (?# matches a double-quote string )
        | '(?<title>([^']|(?<=\w)'(?=\w))+)' (?# matches a single-quote string, allowing quotes in words )
        ) (\s*,\s*|\s+by\s+)?   (?# optionally follow these by ',' or 'by' )
      | 
      (?<title>.*)(\s*,\s*|\s+by\s+) (?# otherwise, everything up to ',' or 'by' )
    )
    \s*(?<artist>.*) (?# everything after this is the artist name )
    $";
    

    Edit:

    I’ve played around a bit with the PHP code, but I can’t get it to use named capturing groups properly. Here is a version using unnamed capturing groups:

    $regex = "/^(?:(?:\"([^\"]+)\"|'((?:[^']|(?<=\\w)'(?=\\w))+)')(?:\\s*,\\s*|\\s+by\\s+)?|(.*)(?:\\s*,\\s*|\\s+by\\s+))\s*(.*)\$/";
    
    preg_match($regex, '"down by the river"', $matches);
    
    print_r($matches);
    

    The title will be in group 1, 2, or 3, and the artist in group 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Struggling to parse this JSON response from http://api.twitter.com/1/trends/current.json using foreach ($json_output->trends[0] as $trend )
Currently struggling with PHP and creating KML from it. I'm using the last.fm API
I´m struggling to understand this concept: I have a fixed size definition: (from http://msdn.microsoft.com/pt-br/library/aa931918.aspx
im struggling with regular expressions in Javascript, they don't seem to start at the
Struggling in vain to extract the value of the Status descendant from an XML
Struggling with styling the mouse over for a button ... I have managed to
Still struggling to understand what best practices are with respect to macros. I'm attempting
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
Been struggling with this simple selector problem a couple of hours now and must
im struggling with syntax here: hopefully this question is v simple, im just miising

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.