I’m trying to solve a Ruby problem using as less code as possible :). I want to check if a given string starts with a varying number of symbols, lets say between one and three ‘a’. That’s easy using reg exp, I can write string.match(\^a{1,3}.*\). But then I have to make changes on the string depending of the exact number of a’s. How can I detect them without using three if’s like that:
do_string_manipulation1 if string.match(/^a.*/)
do_string_manupulation2 if string.match(/^aa.*/)
...
Thanks in advance 🙂
Why not use a modified version of the first regexp and then simply count the a’s it returns? A la
Of course you can also use an array to store routines to call, depending on the number: