Last active
August 29, 2015 14:15
-
-
Save techanon/7110c30c77a95036ada4 to your computer and use it in GitHub Desktop.
A very long and convoluted regex to dissect a properly named file for music.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(?P<originalartist>.+?) -(?: (?P<albumname>(?:(?! - ).)+) -(?: (?<tracknumber>(?:[0-9][0-9])))?)? (?P<songtitle>(?P<songname>(?:(?! \(ft\.).)+) ?(?:\(ft\. (?P<originalfeaturing>(?:[^)]+?(?:(?=\))|, ))+)\))?(?: \((?:(?P<productionartist>(?:(?!\1 ).+)?) )?(?P<songtype>(?:Remix|VIP|Cover|Version|Instrumental|Parody|Mashup))(?: ft\. (?P<additionalfeaturing>(?:[^)]+?(?:(?=\))|, ))+))?\))?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An example of how to construct a filename that the regex can parse in javascript. | |
var filename = originalartist +' - '+ | |
(albumname ? albumname +' - '+ (tracknumber ? tracknumber : '') : '') + | |
(!albumname ? (tracknumber ? tracknumber + ' ' : '') : '') + songname + | |
(featuring ? '(ft. '+ (featuring instanceof Array ? join(', ',featuring) : featuring) +')') + | |
(songtype ? | |
'('+ (additionalartist ? additionalartist : '') + | |
songtype + | |
(additionalfeaturing ? ' ft. '+ | |
(additionalfeaturing instanceof Array ? join(', ',additionalfeaturing) : additionalfeaturing) : '') + | |
')' : '' | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notes: | |
1) The "additionalartist" match MIGHT contain additional text aside from the artist's name. | |
EX: Artist - Song (Second Artist FooBar Remix) | |
The "additionalartist" match would result in 'Second Artist FooBar' and the songtype would be 'Remix' | |
2) If the original "albumname" contains the ' - ' catch sequence, it will stop before that sequence. | |
EX: Artist - Album - Subalbum - Song | |
The "albumname" match would result in 'Album' and not 'Album - Subalbum'. To fix, simply change one of the characters in the catch sequence to something else, or remove a space. | |
EX: Artist - Album . Subalbum - Song | |
The "albumname" match would correctly result in 'Album . Subalbum' | |
3) The "albumname" match must be present for a the "tracknumber" match to fit. Otherwise any "tracknumber" value will spill over to the "songtitle" match. | |
4) Matches that fall inside a literal ( ) pair in the regex MUST be wrapped in a literal ( ) pair or the results will fail for those matches. | |
5) There are probably a couple more notes I'm forgetting... I'll add them if I think of them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment