Using regex to get multiple matches begin with 1 or 2 digits, dot, and space

Viewed 116

I have tried for an hour and I cannot figure this out myself, please help me if you could. I am trying to split the multiple-line text I copy from pdf file into note 1, 2, 3, and 4 ideally or 3 notes between 1. and 2., 2. and 3., and 3. and 4. should also work.

Using the pattern, I can get 4 matches but it only returns the first line of each note.

 ^\d{1,2}\.[ ][a-zA-Z](.*\n)

I tried to add something but it either returns error, nothing matched, or just 1 match.

Sample text:

1. SOMEONE SOMEONE SOMEONE
SOMEONE SOMEONE SOMEONE SOMEONE SOMEONE
SOMEONE SOMEONE SOMEONE SOMEONE SOMEONE
2. SOMETHING SOMETHING SOMETHING
SOMETHING SOMETHING SOMETHING SOMETHING SOMETHING SOMETHING
SOMETHING SOMETHING SOMETHING
SOMETHING SOMETHING SOMETHING
3. SOMEHOW SOMEHOW
SOMEHOW SOMEHOW SOMEHOW SOMEHOW SOMEHOW 
4. SOMEWHERE SOMEWHERE SOMEWHERE SOMEWHERE 
1 Answers

If a negative lookahead is supported, you could repeat matching all lines that do not start with 1-2 digits, space and char a-zA-Z:

^\d{1,2}\.[ ][a-zA-Z].*(?:\r?\n(?!\d{1,2}\.[ ][a-zA-Z]).*)*

Explanation

  • ^ Start of string
  • \d{1,2}\.[ ][a-zA-Z].* Match 1-2 digits, . space and a char a-zA-Z followed by the rest of the line
  • (?: Non capture group
    • \r?\n(?!\d{1,2}\.[ ][a-zA-Z]) Match a newline and the rest of the line if it does not start with the initial pattern using a negative lookahead.
    • .* If that is the case, match the whole line
  • )* Close the group and repeat 0+ times to get all lines

Regex demo

Related