Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda