Extracting the name of a track from the /r/listenToThis subreddit for an IFTTT automation script

I have a list of songs gathered from this subreddit, presented in the following format:

[
  "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",
  "Julia Jacklin - Pressure to Party [Rock] (2019)",
  "The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",
  "Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",
  "$uicideboy$ - Death",
  "SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",
  "Blab - afdosafhsd (2000)",
  "Something strange and badly formatted without any artist [Classical]",
  "シロとクロ「ミッドナイトにグッドナイト」(Goodnight to Midnight - Shirotokuro) - (Official Music Video) [Indie/Alternative]",
  "Victor Love - Irrationality (feat. Spiritual Front) [Industrial Rock/Cyberpunk]"
  ...
]

I'm attempting to extract the title and artist information from each entry using regex, but I'm encountering difficulties.

My initial approach of splitting by "-" only provides the artist, which is cumbersome to deal with.

I also tried using regex, but I can't seem to get it right. Here's what I had for the artist:

/(?<= -{1,2} )[\S ]*(?= \[|\( )/i
and for the title: /[\S ]*(?= -{1,2} )/i.

Each entry consists of a song title with the artist potentially preceding it with one or two dashes. Genres may be enclosed in square brackets and release dates in parentheses. While I don't expect perfect results, I prefer undefined for the artist if parsing is unsuccessful for unusual formats.

For example:

[
  { title: "MYTCH", artist: "Lophelia" },
  { title: "Pressure to Party", artist: "Julia Jacklin" },
  { title: "I'm Going Home", artist: "The Homeless Gospel Choir" },
  { title: "The last night of the world", artist: "Lea Salonga and Simon Bowman" },
  { title: "Death", artist: "$uicideboy$" },
  { title: "Joni Mitchell Tapes", artist: "SNFU" },
  { title: "afdosafhsd", artist: "Blab" },
  { title: "Something strange and badly formatted without any artist" },
  { title: "Goodnight to Midnight", artist: "Shirotokuro" }, // AI might be needed for this
  { title: "Irrationality", artist: "Victor Love" }
]

Answer №1

To extract the title and artist information from your post, you can utilize the following regular expression:

^([^-[\]()\n]+)-* *([^[\]()\n]*)

Check out the Regex Demo here (displayed in PCRE format for better group color visual presentation, but it's compatible with Javascript as well)

For demonstration, here is a snippet of JavaScript code:

const songs = ["Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",
"Julia Jacklin - Pressure to Party [Rock] (2019)",
"The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",
"Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",
"Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz]",
"Death - $uicideboy$",
"SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",
"Title - Aritst (2000)",
"Something strange and badly formatted without any artist [Classical]"]

songs.forEach(song => {
  let matches = /^([^-[\]()\n]+)-* *([^[\]()\n]*)/.exec(song);
  console.log("Title: " + matches[1] + ", Artist: " + matches[2]);
})

Answer №2

To achieve a similar result, you can follow this code snippet:

const tracks = [
  "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",
  "Julia Jacklin - Pressure to Party [Rock] (2019)",
  "The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",
  "Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",
  "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz]",
  "Death - $uicideboy$",
  "SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",
  "Title - Aritst (2000)",
  "Something strange and badly formatted without any artist [Classical]",
];

const regex = /\s*((\[[^\]]+\])|(\(\d+\))).*$/;

const info = tracks.map(track => {
  const split = track.split(/\s+\-+\s+/);
  let title = split[0];
  let artist = split[1];
  if (split.length >= 2) {
    artist = artist.replace(regex, '');
  } else {
    title = title.replace(regex, '');
  }
  return {
    title,
    artist
  }
});
console.log(info);

Answer №3

To achieve the desired outcome, follow the steps below: 1. For Titles, extract the substring starting from the position after '- ' and before ' [' (if present). If ' [' is not found, take the substring until the end.

v.substring(v.indexOf('- ')+1, v.indexOf(' [') !== -1? v.indexOf(' [') : v.length).trim()
  1. For Artists, extract the substring starting from position 0 to the index of '-'

    v.substr(0, v.indexOf('-')).trim()})

Refer to the code snippet below for a working example:

let arr = [
  "Lophelia -- MYTCH [Acoustic Prog-Rock/Jazz] (2019)",
  "Julia Jacklin - Pressure to Party [Rock] (2019)",
  "The Homeless Gospel Choir - I'm Going Home [Folk-Punk] (2019) cover of Pat the Bunny | A Fistful of Vinyl",
  "Lea Salonga and Simon Bowman - The last night of the world [musical] (1990)",
  "$uicideboy$ - Death",
  "SNFU -- Joni Mitchell Tapes [Punk/Alternative] (1993)",
  "Blab - afdosafhsd (2000)",
  "Something strange and badly formatted without any artist [Classical]"
]

let result = arr.reduce((acc, v) => {
  acc.push({
    title: v.substring(v.indexOf('- ')+1, v.indexOf(' [') !== -1? v.indexOf(' [') : (v.indexOf(' (') !== -1? v.indexOf(' (') : v.length)).trim(), 
    artist: v.substr(0, v.indexOf('-')).trim()})
  return acc
}, [])

console.log(result)

Visit the following CodePen link for a live demo: https://codepen.io/nagasai/pen/zQKRXj?editors=1010

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process of redefining the toString method for a function?

I am currently tackling a coding challenge that involves chaining functions. While researching possible solutions online, I noticed that many of them involved using function.toString and overriding the toString method of a function to create a chained add ...

Is TypeScript necessary, or can I simply stick with ES6?

As a client developer using AngularJS in my daily job, we are considering transitioning to TypeScript. After researching TypeScript, I discovered that most JavaScript packages I require need definition type files. This can be inconvenient, especially whe ...

What is the best way to incorporate a loop into a React return statement?

After learning React, I decided to develop the Tic-tac-toe game following the official documentation. There were extra tasks listed below the "Tutorial" section, and one of them was: "Rewrite the Board component to use two loops to generate the squares in ...

Can anyone suggest a method for adding comments and improving the organization of a bower.json file?

Managing a large project with numerous bower dependencies can be challenging. It's often unclear whether these dependencies are still being used or if the specified versions are necessary for a reason. It would be ideal to have the ability to add comm ...

Activate the onclick event for HTML select-options when there is only a single option available

My HTML select dropdown features 5 options, which are a list of car manufacturers. When a user clicks on an option, the onchangeHandler triggers to capture the selected value. Based on this selection, another dropdown displaying car models is shown to the ...

Using Ember's transitionTo method and setting a controller attribute within a callback

I am working with Ember code to transition to a specific route while also setting controllerAttr1 on 'my.route' Upon transitioning to "my.route" using this.get('router').transitionTo("my.route"), I have set up a callback function that ...

Experience the quick sort programming algorithm in action using Vue.js without the hassle of dealing with Promises

I am currently experimenting with the quicksort algorithm visualization using Vue.js for self-educational purposes. However, I am facing some difficulties with async/await implementation. Although array filling and shuffling seem to work fine (although I a ...

angular data binding returning the identifier instead of the content

I have been dealing with managed fields retrieved from a web server in the following format: { "fields":{ "relationshipStatus":[ { "fieldId":4, "name":"Committed" }, { "fieldId":2, ...

Creating a class that can be easily mocked for connecting to mongoDB

I've been attempting to develop a class that connects to MongoDB (and accesses a gridFS connection using gridfs-stream). However, I have encountered two specific problems: Sometimes, I receive the mongo Error server instance in invalid state connect ...

The property 'item' is not found within the specified type 'IntrinsicAttributes & RefAttributes<Component<{}, any, any>>'. Error code: 2322

"react": "^16.12.0", "typescript": "^4.0.3", "next": "^9.4.4" The error being raised by typescript is related to the <Item item={item} key={item.id} urlReferer={urlReferer} /> prop used ...

Angular JS Tab Application: A Unique Way to Organize

I am in the process of developing an AngularJS application that includes tabs and dynamic content corresponding to each tab. My goal is to retrieve the content from a JSON file structured as follows: [ { "title": "Hello", "text": "Hi, my name is ...

Mapping JSON data containing a collection of objects as an observable, rather than as an observable array, is an

When developing my webpage, I needed to receive a viewmodel of a user account via Json. The data includes essential user details such as real name and email address. Additionally, I wanted to display (and modify) the groups that the user belongs to, along ...

The issue arises with the Google Map marker failing to function correctly when pulling data

I've recently started learning Google Maps. It's interesting that markers work and are displayed when statically declared, but not when retrieved from a database. // var markers = [[15.054419, 120.664785, 'Device1'], [15.048203, 120.69 ...

What is the best way to update $state in AngularJs when the user makes changes to the controller?

I am currently working on Angular UI Router and I want to refresh the current state by reloading it and rerunning all controllers for that state. Is there a way to reload the state with new data using $state.reload() and $stateParams? Here is an example ...

Issue encountered while creating a token in a JavaScript Chrome extension and attempting to validate it on a backend Node.js server

Trying to create a token within a chrome extension and utilizing it to authenticate requests to the backend server has proven challenging. While successfully generating a token on the front end, encountering an error when verifying it with the google-auth- ...

Error: Angular is encountering an issue with accessing the property 'push' as it is currently undefined

I have successfully created a news ticker that works well in my codepen example. However, I am encountering an issue with integrating it into my code structure. The error message I am currently receiving is: Cannot read property 'push' of undefi ...

Determine the number of elements chosen within a complex JSON structure

I need to figure out how to count the length of a jsonArray, but I'm stuck. Here's an example to start with: https://jsfiddle.net/vuqcopm7/13/ In summary, if you click on an item in the list, such as "asd1", it will create an input text every t ...

Even when I try to access the properties of the arguments object, it remains empty and has a zero

Why is the arguments object showing a length of zero when I pass parameters to the function, even though I can still access its properties? I am referring to the arguments object that is implemented by all JavaScript functions, allowing you to access the f ...

Scroll horizontally on a webpage using drag with JavaScript

I have been working on a unique horizontal website design that allows users to scroll horizontally by dragging the screen. I managed to successfully implement the horizontal scrolling feature, but I am facing difficulties in adding the horizontal drag-to-s ...

Troubleshooting: Issue with AngularJS $location.path functionality

I have some queries related to the usage of $location.path(/helpmeunderstand). I have a method called login() and when the login credentials are successful, I want to redirect to $location.path(/login/:username). However, instead of displaying the username ...