Anand Murali
Anand Murali

Reputation: 4109

Regex - How to match all text between a given word

This is my text

FUNCTION 1
    Some text
FUNCTION 2
    Some text
FUNCTION 3
    Some text

I want to get the following matches

Match 1

FUNCTION 1
    Some text

Match 2

FUNCTION 2
    Some text

Match 3

FUNCTION 3
    Some text

The pattern that I have come up with so far is

FUNCTION[\s\S]*FUNCTION

Online demo link

Upvotes: 0

Views: 41

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627380

You may use tempered greedy token regex:

FUNCTION(?:(?!FUNCTION)[\s\S])*

See the regex demo

The (?:(?!FUNCTION)[\s\S])* is the token that matches any char ([\s\S]) that is not starting a FUNCTION character sequence. Basically, it is similar to FUNCTION\[\S\s\]*?(?=FUNCTION|$) as far as the final results are concerned, just the lazy matching is turned to a construct that matches up to the next sequence without requiring it to appear at the end, hence we can omit the |$ alternative.

If the word FUNCTION appears on a separate line surrounded with tabs/spaces, you may unroll it as

^[ \t]*FUNCTION[ \t]*(?:\r?\n(?![ \t]*FUNCTION[ \t]*$).*)*

See this regex demo (note the use of the m multiline modifier here).

Upvotes: 1

Related Questions