我想用第一个元音分割一个字符串。如果下面的字母也是元音,那么它必须不会分裂,直到它到达序列中的最后一个元音。例如:
"Auxiliary"
=> ["Au" "xiliary"]
"Doorknob"
=> ["Doo" "rknob"]
"Green"
=> ["Gree" "n"]
我做了:
"Auxiliary".split("u")
# => ["A", "uxiliary"]
比赛结束后我需要分手,而不是之前。我不确定我是否可以做"Auxiliary".split(/[aeiouAEIOU]/)
之类的事情;这只会在第一次发现元音时分裂。
答案 0 :(得分:5)
您可以尝试使用以下模式:
([^aeiou]*[aeiou]+)(.*)
([^aeiou]*[aeiou]+) match and capture zero or more continuous non vowels
followed by one or more vowels
(.*) then capture the remainder of the string
puts "Auxiliary".scan(/([^aeiou]*[aeiou]+)(.*)/i)
puts "Doorknob".scan(/([^aeiou]*[aeiou]+)(.*)/i)
puts "Green".scan(/([^aeiou]*[aeiou]+)(.*)/i)
<强>输出:强>
Au
xiliary
Doo
rknob
Gree
n
在这里演示:
答案 1 :(得分:2)
R = /
\A # match beginning of string
[^aeiou]* # match zero or more non-vowels
[aeiou]+ # match one or more vowels
/ix # case-indifferent and free-spacing regex definition modes
def split_after_vowels(str)
return nil unless str =~ R
i = Regexp.last_match.end(0)
[str[0,i], str[i..-1]]
end
split_after_vowels 'Aardvark'
#=> ["Aa", "rdvark"]
split_after_vowels 'taught'
#=> ["tau", "ght"]
split_after_vowels 'zzzzzz'
#=> nil
split_after_vowels 'zzzzzz sleep'
#=> ["zzzzzz slee", "p"]