VirtualSnake
VirtualSnake

Reputation: 83

Matching multiline regex in Sublime Text

I was tried to write regex in Sublime Text that find three match in

A1
A2
A3
B1
B2
B3
C1
C2
C3

1 match

A1
A2
A3

2 match

B1
B2
B3

3 match

C1
C2
C3

Of course, It can be handled with for example

((?:A\d\n)+|(?:B\d\n)+|(?:C\d\n)+)

Is there more smart approach to do it instead of just enumerating?

Interactive demo

Upvotes: 2

Views: 3085

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

You may use

^([A-Z])\d+(?:\R\1\d+)*

See the PCRE regex demo (SublimeText3 uses PCRE regex engine).

Details

  • ^ - start of a line
  • ([A-Z]) - Group 1: any ASCII uppercase letter (just note that if Case sensitive option is off, this will also match lowercase letters)
  • \d+ - 1+ digits
  • (?:\R\1\d+)* - zero or more repetitions of:
    • \R - a line break
    • \1 - the same letter as in Group 1
    • \d+ - 1+ digits

Upvotes: 5

Michał Turczyn
Michał Turczyn

Reputation: 37337

Try this pattern: ([A-Z])\d\n\1\d\n\1\d.

Demo

Upvotes: 1

Related Questions