Home > Software engineering >  How to capture optional lines between matches using Regex?
How to capture optional lines between matches using Regex?

Time:07-14

I have this expression: (?>Division: (\d ))(?>.*?\")?(.*?(?=\"))?

I would like to capture optional lines (you can see them under "Division: 8") to a group 3 in matches. I haven't got the faintest idea how to capture them, I've tried everything. There is no need to separate them or something, just capture all lines between divisions and place them into the group. All lines between divisions 8 and 9 should be in the optional group of a match 8 (the match 8 is the division 8).

[Divisions]

Division: 1
Division: 2
Division: 3,    Description: "(test1)"
Division: 4
Division: 5
Division: 6,    Description: "test2"
Division: 7
Division: 8
Console,    Events: Fires, Alarams
Printer,    Events: Fires, Alarams
Division: 9
Division: 10

To clarify, I use C#.

The expression: https://regex101.com/r/1OCRHf/1

CodePudding user response:

You might use:

\bDivision: (\d )\b(?:[^\r\n\"]*\"([^\"\r\n]*)\")?((?:\r?\n(?!Division:).*)*)

The pattern matches:

  • \bDivision:
  • (\d )\b Capture group 1, match 1 digits followed by a word boundary
  • (?:[^\r\n"]*"([^"\r\n]*)")? Optionally match a part where capture group 2 matches what is in between the double quotes
  • ( Capture group 3
    • (?:\r?\n(?!Division:).*)* Repeat matching all lines that do not start with Division:
  • ) Close group

Regex demo

  • Related