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

161
Visualizações
regex to find more than one occurence of comment within a block

I have some markdown files in which I follow a certain Q & A format to sync my flashcards from my note-taking software Obsidian to Anki. Somehow, there seems to be messup where there were duplicate questions created in Anki due to newline characters. I want to find these duplicates and delete them out from my note and remove the redundant newline characters which caused these duplicates questions to happen. Here is a sample data file:

### Alias

Q: what check to do before creating an alias?

A: check whether any other command exists by that name using `type new-alias-name`
<!--ID: 1643091253375-->

---

Q: syntax to create an alias?

A: `alias name='string'`
<!--ID: 1645446702965-->

- no spaces before or after the equal sign
- single quotes around the command sequence
![[Pasted image 20220125092954.png]]
<!--ID: 1643091253408-->

---

### Redirection/Piping

Q: output of `> ls-output.txt`

A: the file will be truncated if it exists or it would be created.
<!--ID: 1645087988730-->

- Simply using the redirection operator with no command preceding it will truncate an existing file or create a new empty file
<!--ID: 1643351731943-->

---

Each comment which has a format like <!--ID: 1645446702965--> is a flashcard in Anki. In the 2nd question to create an alias, you can see that there are 2 IDs. I want to delete the first one and remove the newlines after that so that there is only one ID comment that remains.

Here is the regex I am trying - ^A:.+?(<!--ID:\s\d+-->)(?!---).+(<!--ID:\s\d+-->)

Here I am trying to match the Answer sections where the ID comments occur twice. I am using a negative lookahead to allow anything other than ---. But the highlight shows that the regex is reaching too far. I am quite new to Regex and just finished one round of reading 'Mastering Regular expressions by Jeffrey Friedl'. I am still trying to wrap my head around greedy and lazy behaviour. Any guidance to help me move forward on this would be appreciated.

regex snapshot in regex101.com

I have my regex with the data posted here in case anyone want to try: https://regex101.com/r/jBSc8Y/1

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

0

Beside the versions already linked with the comments, the most practical solution might be to use a string replacement with a simple regex like /<!--\s*ID:\s*\d+\s*-->\n+-\s+/g ... where one would match ...

  • <!--\s*ID:\s*\d+\s*--> ... the exact ID format
  • \n+-\s+ ... followed by at least one new line, a hyphen, and a whitespace(-sequence).

The last bullet-point also describes what makes an ID format a duplicate.

Then one just needs to replace the match by - .

const sampleText = `

### Alias

Q: what check to do before creating an alias?

A: check whether any other command exists by that name using \`type new-alias-name\`
<!--ID: 1643091253375-->

---

Q: syntax to create an alias?

A: \`alias name='string'\`
<!--ID: 1645446702965-->

- no spaces before or after the equal sign
- single quotes around the command sequence
![[Pasted image 20220125092954.png]]
<!--ID: 1643091253408-->

---

### Redirection/Piping

Q: output of \`> ls-output.txt\`

A: the file will be truncated if it exists or it would be created.
<!--ID: 1645087988730-->

- Simply using the redirection operator with no command preceding it will truncate an existing file or create a new empty file
<!--ID: 1643351731943-->

`;

const regXDuplicateId = /<!--\s*ID:\s*\d+\s*-->\n+-\s+/g;

console.log(
  sampleText.replace(regXDuplicateId, '- ')
);
.as-console-wrapper { min-height: 100%!important; top: 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