How To Get String Between Two Strings Using Regex In Python

I have two input strings

s1 = "ABCDhelloXYZ"
s2 = "DEFwelcomeXYZ"

I want to get "hello" from s1 and "welcome" from s2. Beginning of the input string can either be "ABCD" or "DEF". The ending will always be "XYZ".

I tried using and I'm able to get the desired output by using 2 different regex like'ABCD(.*?)XYZ')


But how to get the output using single regex, which works for both s1 or s2? I tried'((ABCD)|(DEF))(.*?)XYZ'`)

But it gives different output.



I'd use a non-capturing group, such as:


(?:...) is a non-capturing group. Basically, you are saying "I have no interest in the value".

As pointed out by S3DEV, if your captured text will always be lowercase, you should maybe avoid rather vague .*? and replace it with [a-z]+.

The pipe | character is an or operator, meaning the string will either begin with ABCD or with DEF.