aleha aleha - 9 months ago 21
C# Question

Regex Conditional termination of a template

Pattern always begins from a certain key (in example it is needed beginning)
Value can be multiline.
Pattern can terminate with one of certain keys (in example it is ending1 or ending1). Termination keys can present both or only one of them in input text. If both, then only first of them is needed.

My regex pattern for this example looks like this:

needed beginning([\S\s]*)(?>ending1|ending2)


Here is a Regex101 not working example

Second capturing group is totally wrong, and i do not know how to realize this case

Input text can be like this:

some
other
not
interesting
text
needed beginning
value <--
can be <-- this needed to be captured
multiline <--
ending1
some other values
and other
ending2
some
other
not
interesting
text


Or like this:

some
other
not
interesting
text
needed beginning
value <--
can be <-- this needed to be captured
multiline <--
ending2
some
other
not
interesting
text

Answer Source

This works for you? Updated: https://regex101.com/r/SBNUQG/2

Using regex: needed beginning(.*?)(ending1|ending2)

With option "single line"