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

329
Visualizações
Regex: Find the minimal match... ? not cutting it

I've read so many answers, and it is always to use the ? operator. I'm not sure what I'm missing.

Given a sample string such as:

the beginning stuff:0:evnt:some random stuff:MISTER GREEN:more stuff:1:evnt:again random stuff:MISS SCARLET:the rest of the stuff

What pattern can I use to get the "1" prior to "MISS SCARLET"? Without relying on knowing what's in any of the random stuff parts. I do know it will always be a digit followed by ":evnt" (without quotes) prior to the name we're looking for. In my case, there's no worry about other names getting in the way (ie. it would never be something like :3:evnt:blahblahMISTER GREEN blah blah MISS SCARLET without another x:evnt in between)

I originally came up with:

(\d):evnt.*MISS SCARLET

which gives "0" because it matches back to the index for Mister Green, since .* matches everything. Ok, so ? to the rescue:

(\d):evnt.*?MISS SCARLET

Nope, same result.

How do I get the CLOSEST match to the text in question? I'm sure I'm not wording that correctly, but the example should show what I want. I want to isolate that "1" with MISS SCARLET and "0" if I were looking for MISTER GREEN

Thanks.

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

0

You may use this regex with a negated character class:

(\d+):evnt:[^:]*:MISS SCARLET

Where [^:]* matches 0 or more of any characters that is not :.

RegEx Demo

However if there is a possibility of presence of colon after evnt keyword then you can use this regex with a negative lookahead:

(\d+):evnt:(?:(?!:evnt:).)*?:MISTER GREEN

RegEx Demo 2

Here (?:(?!:evnt:).)*? will make sure match 0 or more of any characters as long as :evnt: doesn't appear anywhere in that text.

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