2016-02-11, 07:42
After months of looking for a decent regex to deal with anime without having to sort to renaming everything, I finally decided I would have a crack at it myself.
Having zero experience with regex, and with the very poor and scattered information in the wiki it wasn't easy, but I think they turned out really great. I spent about 2 days looking at tutorials trying to figure out what the hell was going on, until inspiration struck.
The whole point of what I did is matching the last valid number in the string. Instead of worrying what comes before the episode, which leads to many pitfalls (most regexes fail at Eyeshield 21 123.avi for example), I'm always certain I have the right episode no matter the format. I also don't try to match the stuff that comes after the episode number. If it's a number inside brackets or inside other text it's not a valid episode number. For example in [Commie] Ace of the Diamond - 58v2 [38BCD21A].mkv 38 or 21 are not valid episode numbers, only 58 is (there's an exception for a number followed by v, to account for version numbers).
I do have to make sure I'm dealing with anime in the first place, since this regex doesn't play nice with regular tv shows with seasons. I have all my anime under an anime folder, so I make sure anime is in the path (this can of course be modified to match your folder structure, but keeping shows with absolute ordering and those with seasons separate is a must for this to work). Even if your setup isn't like that, you only have to move files, not rename them for it to work.
I also check that the file has certain extensions. That's completely optional, I just added it because in some shows I have a lot of garbage files that the default regex picks up, so I end up with a lot of duplicates, which is annoying. I do absolutely no bookkeeping on my files, so if you do even the minimum (like not have an extra folder full of junk for each episode), you may not need that and can safely delete it.
Specials need to be numbered according to the TVDB if you're using that scraper, though you don't need to add S00. That's more of a scraper than a regex issue. I still have no clue how Anidb deals with specials, so I can't help you there. So without further ado here it is:
I'd be happy to answer any questions regarding the regex. If something is not being matched I'd also like to hear about it. By design it doesn't match stuff like MyShow ep01.avi. It can be done, but since no files originally come in that format and there might be a slight risk of false positives I decided not to support that MyShow S01E102.avi works without problem though (as long as you only have 1 season), so if you've already renamed some of your stuff, this will still work.
Having zero experience with regex, and with the very poor and scattered information in the wiki it wasn't easy, but I think they turned out really great. I spent about 2 days looking at tutorials trying to figure out what the hell was going on, until inspiration struck.
The whole point of what I did is matching the last valid number in the string. Instead of worrying what comes before the episode, which leads to many pitfalls (most regexes fail at Eyeshield 21 123.avi for example), I'm always certain I have the right episode no matter the format. I also don't try to match the stuff that comes after the episode number. If it's a number inside brackets or inside other text it's not a valid episode number. For example in [Commie] Ace of the Diamond - 58v2 [38BCD21A].mkv 38 or 21 are not valid episode numbers, only 58 is (there's an exception for a number followed by v, to account for version numbers).
I do have to make sure I'm dealing with anime in the first place, since this regex doesn't play nice with regular tv shows with seasons. I have all my anime under an anime folder, so I make sure anime is in the path (this can of course be modified to match your folder structure, but keeping shows with absolute ordering and those with seasons separate is a must for this to work). Even if your setup isn't like that, you only have to move files, not rename them for it to work.
I also check that the file has certain extensions. That's completely optional, I just added it because in some shows I have a lot of garbage files that the default regex picks up, so I end up with a lot of duplicates, which is annoying. I do absolutely no bookkeeping on my files, so if you do even the minimum (like not have an extra folder full of junk for each episode), you may not need that and can safely delete it.
Specials need to be numbered according to the TVDB if you're using that scraper, though you don't need to add S00. That's more of a scraper than a regex issue. I still have no clue how Anidb deals with specials, so I can't help you there. So without further ado here it is:
Code:
<advancedsettings>
<tvshowmatching action="prepend">
<!-- Anime specific matching exclusively for Absolute Ordering. Everything will be matched to season 1, except Specials that will be assigned season 0 -->
<!-- Anime must be contained in a folder called anime (case insensitive) at any dept, to avoid messing with regular TV Shows. You can change it to match your folder sructure. If your anime isn't separate from your other shows then simply delete "Anime" from the regex, but bear in mind it won't play nice with shows with more than 1 season -->
<!-- Files must either have a mkv, mp4, avi or ogm extenion to avoid matching spam files that sometimes come with some downloads like .url and the like. Also optional. Remove (?:(?=.*\.mkv$)|(?=.*\.mp4$)|(?=.*\.avi$)|(?=.*\.ogm$)) if you want to match any extensions. -->
<!-- The last number without brackets not immediately followed by letters other than "v" will be matched. That means shows like Macross 7, Eyeshield 21 or Hunter x Hunter (2011) pose no problem to match. -->
<!-- Specials are automatically asigned to season 0, so there's no need to add "S00" to the filename, but that doesn't affect the match. The file must be in a folder called "Specials" (case insensiive) for each show somewhere inside your anime folder. I recommend numbering after TVDB. -->
<regexp defaultseason="0">(?i)Anime.*Specials(?:(?=.*\.mkv$)|(?=.*\.mp4$)|(?=.*\.avi$)|(?=.*\.ogm$)).+(?=\w)(?<![a-df-z0-9])(\d+)(?<=\d)(?!.*[\\\/])(?![a-uw-z0-9\])}px])</regexp>
<!-- EG: Anime/Kurenai/Specials/[gleam] Kurenai OVA - 01 [OAD][0e73f000].mkv -->
<!-- We are looking for a multi episode with the format Anime/Show 01-02.mkv There can be any number of folders in between and numbers either before or after. It will match only the last set, so in the following file it will pick episodes 08 and 09 for example and not 01 and 12. It will fail on single episodes even if the folder has a range of episodes, so no need to rename anything. -->
<!-- smb://n5200xxx/data/anime/started/hack/[kaa]_hack_twilight_01-12.dvd(complete)/hack_twilight_08-09.dvd(aac)[kaa][06da16fc].ogm -->
<regexp>(?i)()Anime(?:(?=.*\.mkv$)|(?=.*\.mp4$)|(?=.*\.avi$)|(?=.*\.ogm$)).+(?=\w)(?<![a-df-z0-9])(\d+)(\-\d+)(?<=\d)(?!.*[\\\/])(?![a-uw-z0-9\])}px])</regexp>
<!-- This one matches single episodes, always matching the last valid number as the episode. It supports a wide array of formats either with CRC, resolution, hash, etc. or without. -->
<regexp>(?i)()Anime(?:(?=.*\.mkv$)|(?=.*\.mp4$)|(?=.*\.avi$)|(?=.*\.ogm$)).+(?=\w)(?<![a-df-z0-9])(\d+)(?<=\d)(?!.*[\\\/])(?![a-uw-z0-9\])}px])</regexp>
</tvshowmatching>
</advancedsettings>
I'd be happy to answer any questions regarding the regex. If something is not being matched I'd also like to hear about it. By design it doesn't match stuff like MyShow ep01.avi. It can be done, but since no files originally come in that format and there might be a slight risk of false positives I decided not to support that MyShow S01E102.avi works without problem though (as long as you only have 1 season), so if you've already renamed some of your stuff, this will still work.