Replace a long case statement involving regex with a hash
14:05 26 Nov 2025

I'm parsing a number of text files of different formats. Some are csv, some are xml and some even txt. I have a case statement that checks if a certain string is contained in the first 100 bytes of a file in order to identify it. I would like to replace this case statement with a hash-based solution because in the end I will cover 10 or more different file formats and I would like to avoid such a long case statement.

first_bytes = '

I started with the following hash but I'm not sure how to match this.

file_types = {
    '/camt.053.001/' => 'camt053',
    '/camt.052.001/' => 'camt052',
    '/"Auftragskonto";"Buchungstag";"Valutadatum";/' => 'SpK'
}

file_types.keys.select { |key| first_bytes.match(key) }

This doesn't work. It produces an empty array.

regex ruby