如何从字符串中分离元音和辅音,以创建过滤器?
如何用其他字母替换辅音和元音
我想出了这个
(\A[^aeio]{1,3})(\w*)/
在线搜索,但不确定它是如何通过^aeio
的过滤部分来获取辅音的。
答案 0 :(得分:3)
String.tr适合转化文字:
str = "while searching online, but not sure exactly how it works past the filtering part of ^aeio, to get consonants."
p str.tr('aeiou', '')
#=> "whl srchng nln, bt nt sr xctly hw t wrks pst th fltrng prt f ^, t gt cnsnnts."
p str.tr('^aeiou', '') # the ^ negates
#=>"ieeaioieuoueeaoioaeieiaoaeiooeooa"
p str.tr('aeiou', 'eioua')
#=>"wholi sierchong unloni, bat nut sari ixectly huw ot wurks pest thi foltirong pert uf ^eiou, tu git cunsunents."
答案 1 :(得分:1)
你的意思是这样分开吗?
1.9.3-p327 > s = "abcqwertyaeiouvbnmi"
=> "abcqwertyaeiouvbnmi"
1.9.3-p327 > s.split(/([aeiou]+)/)
=> ["", "a", "bcqw", "e", "rty", "aeiou", "vbnm", "i"]
如果是这样,那么你可以循环遍历生成的数组,直接替换字符。
答案 2 :(得分:1)
s = "iamagoodboy"
v,c = s.chars.partition{|i| ["a","e","i","o","u"].include?(i)}
p v #=> ["i", "a", "a", "o", "o", "o"]
p c #=> ["m", "g", "d", "b", "y"]
现在,您可以根据需要对v
和c
进行迭代。