0

如何将元音和辅音从字符串中分离出来,以创建过滤器?

如何用其他字母替换辅音和元音

我想出了这个

(\A[^aeio]{1,3})(\w*)/

在线搜索时,但不确定它是如何通过 , 的过滤部分^aeio来获取辅音的。

4

3 回答 3

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."
于 2013-05-25T18:35:55.690 回答
2

你的意思是这样分开吗?

1.9.3-p327 > s = "abcqwertyaeiouvbnmi"
 => "abcqwertyaeiouvbnmi"
1.9.3-p327 > s.split(/([aeiou]+)/)
 => ["", "a", "bcqw", "e", "rty", "aeiou", "vbnm", "i"]

如果是这样,那么您可以在生成的数组中循环替换字符。

于 2013-05-25T18:24:54.840 回答
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

于 2013-05-25T18:29:15.477 回答