So I’ve managed to invent some Black Magic using regex. I’m attempting to parse some command line arguments, and I want to accept any argument in the format test[somethinginhere] where the ‘somethinginhere’ is the name of a test to run.
This is what the code looks like. Theres more code after it that actually performs functions with the booleans, but this is the part that processes the arguments.
ARGV.each do |arg|
case arg.downcase
when "help"
@help = true
when "install"
@install = true
when "deploy"
@deploy = true
when "configure"
@configure = true
when /^test\[([a-z]+)\]$/
@test << arg.downcase[/^test\[([a-z]+)\]$/,1]
else
@usage = true
end
end
Now here’s where the black magic comes in: this program will run properly for any test name that doesn’t include a lowercase ‘s’. The following will pass:
$ ./AutoTest.rb test[a]
Running Test "ATest"
$ ./AutoTest.rb test[b]
Running Test "BTest"
$ ./AutoTest.rb test[anything]
Running Test "AnythingTest"
The following will fail:
$ ./AutoTest.rb test[s]
Usage: AutoTest [help|install|deploy|configure|test[*]]
But this will pass:
$ ./AutoTest.rb test[S]
Running Test "STest"
Actually, anything with a lowercase s inside of it will fail:
$ ./AutoTest.rb test[user]
Usage: AutoTest [help|install|deploy|configure|test[*]]
Unless that s is capitalized:
$ ./AutoTest.rb test[uSer]
Running Test "UserTest"
The only conclusion I can then come to is that this regex has tapped into the secrets of Black Magic, and at this point is becoming so powerful that it could even parse HTML on its own.
But seriously, what’s going on here?
Brackets with a lowercase s in them have special meaning in bash. I discovered this by doing the following:
This applies to an s anywhere inside the brackets
Still have no idea where this black magic is coming from, but the solution to my problem is to not use brackets anymore.