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

178
Visualizações
RegEx to match whole line after nth occurrence of a string

Long time lurker, first time poster! Thanks to anyone in advance that can help.

I'm using Node.JS and Electron to make a little desktop app that manages the values in a game file (which is from a custom file type, basically a glorified INI file). With the idea of letting people customise the file in a nice UI as opposed to combing through hundreds of lines of code. Below is a sample from the file:

setting $Low
    prop $Config A_STRING_HERE "5, 10, 15, 20"

setting $Medium
    prop $Config A_STRING_HERE "10, 20, 40, 80"

setting $High
    prop $Config A_STRING_HERE "20, 40, 80, 160"

setting $VeryHigh
    prop $Config A_STRING_HERE "40, 80, 160, 320"

How would I go about matching, for example, the "setting $High" (3rd) occurrence containing "A_STRING_HERE" until the end of the line. I'm hoping to match just:
A_STRING_HERE "20, 40, 80, 160".

The values are dynamic and will change a lot, so matching the line with the values isn't necessarily an option.

  • \A_STRING_HERE.+\g will match all the occurrences to the end of the line.
  • \A_STRING_HERE.+\ will match the first occurrence to the end of the line.
  • \A_STRING_HERE.+\?????? will match the 3rd occurrence to end of the line?

Thanks again and all the best!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can probably try this regex:

(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*

Explantion

  • (?<=) - Represents positive lookbehind.
  • setting \$High\n\s*?prop \$Config - The text that separates this match with other matches. You can probably omit setting part to make it more concise.
    • \n - Represents new line.
    • \s*? - Lazily matches 0 or more white spaces. You can adjust it according to your need.
  • .* - Matches everything except \n. You can be specific here as well if you want to include numbers only.
  • \g - Please note only global flag is used.

You can find the implementation of the above regex in here.

Image Explanation: Image

const regex = /(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*/g;

// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=setting \\$High\\n\\s*?prop \\$Config )A_STRING_HERE.*', 'g')

const str = `setting \$Low
    prop \$Config A_STRING_HERE "5, 10, 15, 20"
setting \$Medium
    prop \$Config A_STRING_HERE "10, 20, 40, 80"
setting \$High
    prop \$Config A_STRING_HERE "20, 40, 80, 160"
setting \$VeryHigh
    prop \$Config A_STRING_HERE "40, 80, 160, 320"`;
console.log(str.match(regex)[0]);

about 4 years ago · Juan Pablo Isaza 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