Use a string to hold these characters and interpolate them into regular expressions as needed. Ruby tries to cover some bases with (?mix:) , but it does not expect the regular expression to be part of the character set inside another regular expression.
Background Information
Here's what actually happens:
In many cases, if you interpolate a regular expression into a regular expression, this makes sense. Like this
a = /abc/ #/abc/ b = /#{a}#{a}/ #/(?-mix:abc)(?-mix:abc)/ 'hhhhabcabchthth'.gsub(/abcabc/, '_') # "hhhh_hthth" 'hhhhabcabchthth'.gsub(b, '_') # "hhhh_hthth"
It works as expected. The whole thing (?-mix: is a way to encapsulate the rules for a , just in case b has different flags. a case sensitive because this is the default. But if b was set to case insensitive, the only way for a continue matching with what he compared earlier is to make sure that it is case sensitive using -i . Everything inside (?-i:) after the colon will be case sensitive. This is made more clear from the following
e = /a/i
You can see that when interpolating e into something, you now have (?i-mx:) . Now i is to the left of - , which means that it turns off case insensitivity, and is not turned off (temporarily) so that e matches as usual.
In addition, in order to avoid spoiling the capture order, it is added (?: To create an unoccupied group. All this is a crude attempt to turn the variables a and e into line with what you expect from them when you insert them into a larger regular expression.
Unfortunately, if you put it in coincidence with the character set, that is, [] , this strategy completely fails. [(?-mix:)] now interpreted in completely different ways. [^?-m] indicates everything that is NOT between "?" and "m" (inclusive), which means, for example, that the letter "c" is no longer in your character set. This means that "c" is not replaced by an underscore, as you see in your example. You can see the same thing as the letter "x". It is also not replaced by an underscore, because it is inside a set of characters with a negation and therefore does not match matching characters.
Ruby doesn't bother with parsing a regular expression to understand that you are interpolating your regular expression into a character set, and even if that were the case, you would still have to parse the variable v to find out that it is also a character set, and therefore all that what you really need to do is take the characters from the character set in v and put them with all the other characters.
My advice is that since aeiouAEIOUäöüÄÖÜ is just a bunch of characters, you can save it in a string and interpolate into any character set in a regular expression. And be careful to interpolate regex into regex in the future. Avoid this if you are really not sure what he is going to do.