Solution
Your requirement is similar to the one in this question: match and capture multiple instances of a pattern between a prefix and a suffix. Using the method as described in this answer of mine:
(?s)(?:<context>|(?!^)\G)(?:(?!</context>|ab).)*ab
Add capturing group as you need.
Caveat
Note that the regex only works for tags that are only allowed to contain only text. If a tag contains other tags, then it won't work correctly.
It also matches ab
inside <context>
tag without a closing tag </context>
. If you want to prevent this then:
(?s)(?:<context>(?=.*?</context>)|(?!^)\G)(?:(?!</context>|ab).)*ab
Explanation
Let us break down the regex:
(?s) # Make . matches any character, without exception
(?:
<context>
|
(?!^)\G
)
(?:(?!</context>|ab).)*
ab
(?:<context>|(?!^)\G)
makes sure that we either gets inside a new <context>
tag, or continue from the previous match and attempt to match more instance of sub-pattern.
(?:(?!</context>|ab).)*
match whatever text that we don't care about (not ab
) and prevent us from going past the closing tag </context>
. Then we match the pattern we want ab
at the end.