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

109
Visualizações
Matching a String Between Delimiters if and Only if it Contains a Specific ID Using REGEX

I am trying to match a string between delimiters if and only if it contains the id I am looking for. For example suppose I have a text file containing several entries like the following:

 /id:12345, comment:"test @#$%7 *<", date:JUN-06-21/;/comment:"@#rehj%fh^?*<", date:MAR-15-20, id:11333/;/date:AUG-22-18, id:44618, comment:"&%$@#^?*!!/;

Let's say I want to match an entry that has the ID 44618 using REGEX. What makes this difficult is that ID can appear at the beginning, in the middle, or in the last position. The following is the REGEX I have so far but it's not working.

    \/\w[a-zA-Z0-9,:]*?\s?(id:44618,)\s?(\/\w[a-zA-Z0-9:;/])*
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If you need only specific ID, just write in regex id and set flag "g"-global and "m" multiline yourString.match(/id:747484/gm) And for different ids yourString.match(/id:\d+/gm)

about 4 years ago · Juan Pablo Isaza Relatório

0

Used regex to get whole entry where id can be in any position of the entry:

/\/[^;]*?\bid:44618\D[^;]*?;/

Regex in context and testbench:

const input = '/id:12345, comment:"test @#$%7 *<", date:JUN-06-21/;' + 
              '/comment:"@#rehj%fh^?*<", date:MAR-15-20, id:11333/;' +
              '/date:AUG-22-18, id:44618, comment:"&%$@#^?*!!/;';

const regex = /\/[^;]*?\bid:44618\D[^;]*?;/;           

alert(input.match(regex)[0]);

Output from alert, printing the whole entry:

/date:AUG-22-18, id:44618, comment:"&%$@#^?*!!/;
about 4 years ago · Juan Pablo Isaza Relatório

0

The pattern does not match anything because the first character class [a-zA-Z0-9,:] does not contain all the allowed characters to match until the occurrence of id:44618 in the example string.

You could extend it for example like this but that will not match until the delimiter /;

Another think to note is that if the id is at the start like /id:44618 then matching the first word character like \/\w in your pattern will prevent from matching the id at all as the first character is already consumed.

You could extend the character class at the end of the pattern with all the allowed characters, but as it can also be a comment, you don't know the possible characters up front.

If the comment field is not before the id field, what you might do is assert the word character after the opening / using a positive lookahead (?=\w) and then match the id and match as least as possible chars .*? until the closing /;

\/(?=\w)[a-zA-Z0-9,:;\s-]*?(id:44618).*?\/;

Regex demo

If there are no other / allowed, you might also use a negated character class [^/]* matching any char except the forward slash

\/[^/]*(id:44618)[^/]*\/;

Regex demo

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