Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
How to split each 2 word in string into array - Ruby?

I want to split a string to an array which include each 2 word of original string as below:

Ex:
str = "how are you to day"
output = ["how are", "are you", "you to", "to day"]

Any one can give solution? thank so much!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Input

str = "how are you to day"

Code

p str.split(/\s/)
     .each_cons(2)
     .map { |str| str.join(" ") }

Output

["how are", "are you", "you to", "to day"]
over 4 years ago · Santiago Trujillo Relatório

0

Here is one approach, which uses a regex trick to duplicate the second through second to last words in the input string:

input = "how are you to day"
input = input.gsub(/(?<=\s)(\w+)(?=\s)/, "\\1 \\1")
output = input.scan(/\w+ \w+/).flatten
puts output

This prints:

how are
are you
you to
to day
over 4 years ago · Santiago Trujillo Relatório

0

Here are a couple ways to do that. Both use the form of String#gsub that takes a regular expression as its argument and no block, returning an enumerator. This form of gsub merely generates matches of the regular expression; it has nothing to do with string replacement.

str = "how are you to day"

Use a regular expression that contains a positive lookahead

r = /\w+(?=( \w+))/
str.gsub(r).with_object([]) { |s,a| a << s + $1 }
  #=> ["how are", "are you", "you to", "to day"]

I've chained the enumerator str.gsub(r) to Enumerator#with_object. String#gsub is a convenient replacement for String#scan when the regular expression contains capture groups. See String#scan for for an explanation of how it treats capture groups.

We can write the regular expression in free-spacing mode to make it self-documenting.

r = /
    \w+       # match >= 1 word characters
    (?=       # begin a positive lookahead
      ( \w+)  # match a space followed by >= 1 word characters and save
              # to capture group 1
    )         # end positive lookahead
    /x        # invoke free-spacing regex definition mode

Enumerate pairs of successive words in the sting

enum = str.gsub(/\w+/)
loop.with_object([]) do |_,a|
  a << enum.next + ' ' + enum.peek
end
  #=> ["how are", "are you", "you to", "to day"]

See Enumerator#next and Enumerator#peek. After next returns the last word in the string peek raises a StopIteration exception which is handled by loop by breaking out of the loop and returning the array a. See Kernel#loop.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda