Sto cercando di trovare un modo per farmi creare dinamicamente un oggetto regexp da una stringa (preso dal database) e quindi usarlo per filtrare un'altra stringa. Questo esempio serve per estrarre i dati da un messaggio di commit git, ma in teoria qualsiasi espressione regexp valida potrebbe essere presente nel database come stringa.Dynamic Regex in Ruby
Cosa succede
>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n"
>> r = Regexp.new("[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+")
>> string[r]
=> nil
Quello che voglio che accada
>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n"
>> string[/[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/]
=> "Project: Revision 123456 committed by Me"
Perfetto, grazie - ho capito che dovevo fare qualcosa di sottilmente sbagliato. – davidsmalley