Extracting components of a filename
The string looks like a filename, so you should use the library functions for handling file paths instead of rolling your own.

File.basename(str, '.txt').split('_')