||
is not OR in RegEx, and inside the []
, there's no need OR.
You can simply write your regex like this: /[[\w&&[^aeiou]]\W]/
. (update: or just /[^aeiou]/
)
On the other hand, &&
is Class Intersection.
Examples:
arr
#=> ["Hello!", "How", "are", "you?", "[]||&\\", "My", "name", "is", "Bob;", "what", "is", "yours?"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]\W]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]|]/).join; end # | inside [] will be read literally.
#=> ["Hll", "Hw", "r", "y", "||", "My", "nm", "s", "Bb", "wht", "s", "yrs"]
arr.map do |word| word.scan(/[[\w&&[^aeiou]]||]/).join; end
#=> ["Hll", "Hw", "r", "y", "||", "My", "nm", "s", "Bb", "wht", "s", "yrs"]
## Note this one, it is OR now:
arr.map do |word| word.scan(/[\w&&[^aeiou]]|\W/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
And as Mr. Swoveland correctly pointed out in comments, /[\W||\w&&[^aeiou]]/
is essentially the same as /[^aeiou]/
, because the latter class actually includes \W
.
Also you might want to add i
flag to be case insensitive:
arr = %w(Hello! How are you? []||&\\ hELLO My name is Bob; what is yours?)
arr.map do |word| word.scan(/[\W||\w&&[^aeiou]]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hELLO", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[^aeiou]/).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hELLO", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]
arr.map do |word| word.scan(/[^aeiou]/i).join; end
#=> ["Hll!", "Hw", "r", "y?", "[]||&\\", "hLL", "My", "nm", "s", "Bb;", "wht", "s", "yrs?"]