I want to extract season and episode from a filename in C. For example, if the input string is "Game.of.Thrones.S05E02.720p.HDTV.x264-IMMERSE.mkv", then I want to extract the substring "S05E02" out of it.
At the moment, I'm using a very naive approach for matching characters one at a time. Concretely, I am finding 'S' and then checking if the next two characters are both numbers between '0' and '9' and then the subsequent character is 'E' and the next two characters to 'E' are also between '0' and '9'.
// Return index if pattern found. Return -1 otherwise
int get_tvshow_details(const char filename[])
{
unsigned short filename_len = strlen(filename);
for (int i = 0; i < filename_len-5; ++i) {
char season_prefix = filename[i];
char episode_prefix = filename [i+3];
char season_left_digit = filename[i+1];
char season_right_digit = filename[i+2];
char episode_left_digit = filename[i+4];
char episode_right_digit = filename[i+5];
if ((season_prefix == 'S' || season_prefix == 's')
&& (episode_prefix == 'E' || episode_prefix == 'e')
&& (season_left_digit >= '0' && season_left_digit <= '9')
&& (season_right_digit >= '0' && season_right_digit <= '9')
&& (episode_left_digit >= '0' && episode_left_digit <= '9')
&& (episode_right_digit >= '0' && episode_right_digit <= '9')) {
printf("match found at %d\n", i);
return i;
}
}
return -1;
}
Is there a more efficient way in C to extract the following pattern: S<2_digit_number>E<2_digit_number> from any tv show filename?