Ad

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 re.search and I'm able to get the desired output by using 2 different regex like

re.search(r'ABCD(.*?)XYZ')

and

re.search(r'DEF(.*?)XYZ')

But how to get the output using single regex, which works for both s1 or s2? I tried

re.search(r'((ABCD)|(DEF))(.*?)XYZ'`)

But it gives different output.

Ad

Answer

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

^(?:ABCD|DEF)(.*?)XYZ$

(?:...) 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.

Ad
source: stackoverflow.com
Ad