Search code examples
regexnotepad++

Search consecutive schedule in Notepad++ using RegEx multiline


My requirement is to search on Notepad++, sample text shown below:

UM001 Uddharana Madhava

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: Bengali

Date: 4/6/2021, Friday
Topic: Bangla Economics
Language: Bengali

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: English

CJ009 Cidananda Janardana

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: Telugu

The search result should highlight

UM001 Uddharana Madhava

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: Bengali

Date: 4/6/2021, Friday
Topic: Bangla Economics
Language: Bengali

It should not highlight from other teachers schedule

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: English

CJ009 Cidananda Janardana

Date: 3/6/2021, Thursday
Topic: Bangla Economics
Language: Telugu
Language(.+?)Date

The above search pattern is not working so I tried

Language(.+?)(?![A-Z][A-Z]\d{3})Date
Language(.+?)(?![A-Z][A-Z]\d{3})(.+?)Date

These two are also not working. Please suggest how can I achieve the desired result. Here is what I am looking for, if there is a name in between Language and date, that should not be captured. Here is a link of image which shows my requirement


Solution

  • To highlight only parts with multiple Date parts:

    ^[A-Z][A-Z]\d{3}\b\h+\S.*\s*(?>\RDate:\h.*(?:\R(?!Date:|[A-Z][A-Z]\d{3}\b).*)*){2,}
    

    The pattern matches:

    • ^ Start of string
    • [A-Z][A-Z]\d{3}\b Match the format of the values UM001 for example
    • \h+\S.*\s* Match 1+ horizontal whitespace chars, at least a single non whitespace char and the rest of the line. Then match optional whitespace chars, that could also match a newline
    • (?> Atomic group
      • \RDate:\h.* Match a newline, Date: and the rest of the line
      • (?:\R(?!Date:|[A-Z][A-Z]\d{3}\b).*)* Match all lines that do not start with either Date: or a pattern like the start value
    • ){2,} Close the group and repeat 2 or more times

    See a regex demo.