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

185
Visualizações
Break string into words using scan method + regexp, if word has `'` character, drop this character and everything after it
sample_string = "let's could've they'll you're won't"
sample_string.scan(/\w+/)

Above gives me:

["let", "s", "could", "ve", "they", "ll", "you", "re", "won", "t"]

What I want:

["let", "could", "they", "you", "won"]

Been playing around in https://rubular.com/ and trying assertions like \w+(?<=') but no luck.

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

0

Given:

> sample_string = "let's could've they'll you're won't"

You can do split and map:

> sample_string.split.map{|w| w.split(/'/)[0]}
=> ["let", "could", "they", "you", "won"]
over 4 years ago · Santiago Trujillo Relatório

0

You can use

sample_string.scan(/(?<![\w'])\w+/)
sample_string.scan(/\b(?<!')\w+/)

See the Rubular demo. The patterns (they are absolute synonyms) match

  • (?<![\w']) - a location in the string that is not immediately preceded with a word or ' char
  • \b(?<!') - a word boundary position which is not immediately preceded with a ' char
  • \w+ - one or more word chars.

See the Ruby demo:

sample_string = "let's could've they'll you're won't"
p sample_string.scan(/(?<![\w'])\w+/)
# => ["let", "could", "they", "you", "won"]
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