Mr. Baldan Mr. Baldan - 2 months ago 23
Python Question

To extract a string from among two others

This forms part of a hobby personal project for developing a knowledge description language.
These the text I want to extract strings:

begin car part chassis engine wheels begin motorbike part

chassis engine wheels begin motorbike part wheels chassis

engine begin tree part roots branches stem leaves begin light

bulb part spile filament crystal begin coin part corp begin pen

part ball pipe button begin glasses part mount

eyeglasses begin motorbike part chassis engine wheels

So I expect to obtain using some kind of regex an output something like these:

Every word of the before list is obtained from the middle of two repeated words, "begin" and "part". It is not necessary that you have an output format like the one shown here based on a list, but I want to separate the mentioned words in some way.
Thank you very much for your help.


I think this was the easiest solution to your problem for me.

re.findall(r'begin (\w+)', text)
# ['car', 'motorbike', 'motorbike', 'tree', 'light', 'coin', 'pen', 'glasses', 'motorbike']