Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby访问不带数组的条件行中字符串的非元音_Ruby_String_Conditional Statements - Fatal编程技术网

ruby访问不带数组的条件行中字符串的非元音

ruby访问不带数组的条件行中字符串的非元音,ruby,string,conditional-statements,Ruby,String,Conditional Statements,我需要打开一个字符串,该字符串包含(除其他字母外)一个由3个字母组成的序列,其中包括一个非元音字母、一个“o”和同一个非元音字母 进入 仅包含(其他字母和)非元音的字符串 像 我希望我的代码尽可能紧凑,并且只使用字符串方法 str.each_char { | i | if i == /[^aeiou]/ and i == str[i.index + 2] and str[i.index + 1] == "o" str = str.delete(str.slice(str[i.index + 1]

我需要打开一个字符串,该字符串包含(除其他字母外)一个由3个字母组成的序列,其中包括一个非元音字母、一个“o”和同一个非元音字母 进入 仅包含(其他字母和)非元音的字符串

我希望我的代码尽可能紧凑,并且只使用字符串方法

str.each_char { | i | if i == /[^aeiou]/ and i == str[i.index + 2] and str[i.index + 1] == "o"
str = str.delete(str.slice(str[i.index + 1], 2))
end
}

输出为未更改的字符串。提前谢谢。

我想知道您是否可以使用
map
以一种功能更强大的非破坏性方式来实现这一点,是的,您可以,但不能比其他答案更简洁:

R = /
    ([^aeiou]) # match a consonant in capture group 1
    o          # match an 'o'
    \1         # match the contents of capture group 1
    /x         # free-spacing regex definition mode

def my_method(str)   
  str.gsub(R,'\1')
end

my_method "my dog kok has fleas"
  #=> "my dog k has fleas" 
my_method "much momentum"
  #=> "much mentum" 
my_method "'plolly' is not a word"
  #=> "'plly' is not a word" 
my_method "abkokcmomloljkde"
  #=> "abkcmljkde" 
my_method "bub"
  #=> "bub" 
str = "iskakmomlol"
VOWEL    = /[aeiou]/
RESONANT = /[^aeiou]/
str.chars.map.with_index { |c, i| 
  prevb, prev, nxt, scnd = str[i - 2], str[i - 1], str[i + 1], str[i + 2]
  if i > str.length - 1 || i == 0 then c
  elsif c =~ RESONANT && nxt =~ VOWEL && c == scnd then c
  elsif c =~ VOWEL && prev =~ RESONANT && nxt =~ RESONANT
  elsif c =~ RESONANT && prevb == c && prev =~ VOWEL
  else  c
  end
}.compact.join # "iskmljk"

实际上,这可以缩短:

R = /([^aeiou])[aeiou]\1/
str.chars.map.with_index { |c, i| 
  c unless str[i-1..i+1][R] || str[i-2..i][R]
}.compact.join # "iskmljk"
我想既然“o”是要接受的序列中的固定字符,我就可以使用count变量,看看它旁边的字符是否彼此相等,是否为非元音。我还发现slice还可以传递两个参数,这样它就可以从一个偏移开始切片,并在给定的长度之后停止

index = 0
  while index < str.length
    index = index + 1
    if str[index] == "o" and str[index-1] == str[index+1] and str[index-1] != /[^aeiou]/ 
      str.slice!(index, 2)
index=0
而指数
如果
str=“kokok”
则返回
“kk”
。我不知道询问者是否想要这个,或者
“kok”
。dongo?是的,我的编辑处理这个问题,因为一个k,如果需要,由dongo决定,通过组合我们的答案,他应该能够得到他想要的。应该接受“kok”的顺序,并在读取字符串的其余部分之前将其转换为“k”,所以“kokkok”返回“kk”,而“kokok”返回“kok”。谢谢你的澄清,dongo。两点。1.最好通过编辑你的问题来澄清,而不是试图在评论中这样做。其他读者可能也有同样的问题,但却错过了你的解释性评论。2.Peter被告知,所以对他的问题留下了评论,但我没有。(我只是碰巧注意到了。)要通知我,你需要包括“@CarySwoveland”(或者只是前面有符号的“Cary”)。评论最多只能有一个符号和用户名。有什么问题?请阅读“及”。
index = 0
  while index < str.length
    index = index + 1
    if str[index] == "o" and str[index-1] == str[index+1] and str[index-1] != /[^aeiou]/ 
      str.slice!(index, 2)