Assuming the following data structure:
data = 'Months = { "January" => 1, "February" => 2, "March" => 3 }'
This will scan through it to find the numbers associated with the month names:
months_to_find = %w[January February March] months_re = Regexp.new( '(%s) .+ => \s+ (\d+)' % months_to_find.join('|'), Regexp::IGNORECASE | Regexp::EXTENDED ) Hash[*data.scan(months_re).flatten]['January'] # => 1
The magic occurs here:
months_re = Regexp.new( '(%s) .+ => \s+ (\d+)' % months_to_find.join('|'), Regexp::IGNORECASE | Regexp::EXTENDED )
which creates this regex:
/(January|February|March) .+ => \s+ (\d+)/ix
Add additional months to months_to_find.
That will continue to work if the data is changed to:
data = 'Months = { "The month is January" => 1, "The month is February" => 2, "The month is March" => 3 }'