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

97
Visualizações
How to find specific matches in a string using regex

I have strings like

View_Export_US_Horizontals_Ultimate_DirectionalSurveys

I want to extract matches like

[["US_Horizontals", "Ultimate", "DirectionalSurveys"]] //String Ultimate may or may not be present

I have the following RegEx, /^View_Export_(US\_(GoM|Horizontals|Onshore))((_Ultimate)?)_(\w+)/

but i get the following matches array

[["US_Horizontals", "Horizontals", "_Ultimate", "_Ultimate", "DirectionalSurveys"]]

How do i skip strings like Horizontals & _Ultimate and just get an array like

[["US_Horizontals", "Ultimate", "DirectionalSurveys"]]

Or

[["US_Horizontals", "DirectionalSurveys"]]

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

0

You can use

\AView_Export_(US_(?:GoM|Horizontals|Onshore))(?:_(Ultimate))?_(\w+)

See the regex demo. Details:

  • \A - start of string (^ in Ruby regex means start of any line)
  • View_Export_ - a fixed string
  • (US_(?:GoM|Horizontals|Onshore)) - Group 1: US_ string and then either GoM, Horizontals or Onshore words
  • (?:_(Ultimate))? - an optional sequence of an underscore and an Ultimate word
  • _ - an underscore
  • (\w+) - Group 3: any one or more word chars.

See the Ruby demo:

string = "View_Export_US_Horizontals_Ultimate_DirectionalSurveys"
rx = /\AView_Export_(US_(?:GoM|Horizontals|Onshore))(?:_(Ultimate))?_(\w+)/
one, two, three = string.match(rx).captures

puts one   #=> US_Horizontals
puts two   #=> Ultimate
puts three #=> DirectionalSurveys
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