Search code examples
regexregex-group

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.

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