Skip to content
Advertisement

How to extract the value between the key using RegEx?

I have text like:

"abababba"

I want to extract the characters as a list between a. For the above text, I am expecting output like:

['b', 'b', 'bb']

I have used:

re.split(r'^a(.*?)a$', data)

But it doesn’t work.

Advertisement

Answer

The ^ and $ will only match the beginning and end of a line, respectively. In this case, you will get the desired list by using the line:

re.split(r'a(.*?)a', data)[1:-1]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement