Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex that stop at a line

I’m trying to build a regex that stop when a line is equal to "— admonition".

For example, I have :

??? ad-question Quels sont les deux types de bornages ?

Il y en a deux :

- Le bornage amiable.

- Le bornage judiciaire.

test

--- admonition

I can have the same capture format multiple time on a page.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to retrieve (in every match) in a first group :

Quels sont les deux types de bornages ?

and in a second :

Il y en a deux :

  • Le bornage amiable.

  • Le bornage judiciaire.

test

I tried :

^\?{3} ad-question {1}(.+)\n*((?:\n(?:^[^#].{0,2}$|^[^#].{3}(?<!---).*))+)

or

^\?{3} ad-question {1}(.+)\n*((?:\n(?:^[^\n#].{0,2}$|^[^\n#](?<!----).*))+)

but it didn’t stop at "\n— admonition" and it took the new line between the two group.

Is someone can help me build this regex ?

ps : I must have a new line between the two group and between group 2 and "—- admonition". So these lines must be avoid in the groups.

Thanks for your help.

>Solution :

If you want 2 capture groups without matching the newlines in between the groups, but there must be at least a whole empty line in between the groups:

^\?{3} ad-question (.+)\n{2,}((?:(?!---).*\n)*?)\n+---

The pattern matches:

  • ^ Start of string
  • \?{3} ad-question Match ??? ad-question
  • (.+) Capture group 1, match the whole line
  • \n{2,} Match 2 or more newlines, so that there is at least an empty line in between
  • ( Capture group 2
    • (?:(?!---).*\n)*? Repeat as least as possible matching all lines and the newline, that do not start with —
  • ) Close group 2
  • \n+--- Match 1 or more newlines and ---

Regex demo

If there should be at least a single newline present:

^\?{3} ad-question (.+)\n+((?:(?!---).*\n)*?)\n*---

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading