RegEx - One Group for each set of lines

Coyttl :

Say I have a text file that has content similar to the following:

021 Line one of section A.
021 Line two of Section A.
021 Line three of section A.
021 Part two of Line three of Section A.
021 We just skipped line four, but that's okay.
021 Back to line six.
Non-formatted lines to be ignored. This can be from 0 lines, to any number of lines, and the content can be any text.
033 Line 1 of Section B
033 Line 2 of Section B
033 Okay, that's enough.

Is it possible, in regex, to give me two groups, the first containing all the lines that start with 021, and the second all the lines starting with 033?

The line tokens would vary, but would always be \d{3}.

The fourth bird :

You might capture the digits at the start in a capturing group and use a backreference \1 to that group while repeating it.

This will give you the matches where the digits at the start are the same.

^(\d{3}) .*(?:\r?\n\1.*)*

Regex demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133652&siteId=1