
On Tue, 29 Mar 2022 at 18:13, StrikerOmega <oddochentaycinco850@gmail.com> wrote:
The grab function would find the index of the first occurrence of the "start" string in the parent string and then the next occurrence of the "end" string starting from that index and return the substring between those.
This sounds like a really good job for preparsing. Take your string, parse it according to your rules, and build a list or dict of the cooked results. Then you can look up in that very easily and efficiently. Going back to the string every time tends to be inefficient, but a single pass that gives you a convenient lookup table is both easier to work with and easier to execute. In your example: sample=""" fruit:apple tree:[Apple tree] quantity:{5} quantity:{3} """ I'd start by splitting it into lines, then for each line, partitioning it on the colon, thus giving you a keyword and a value. (I'm not sure what it means to have quantity 5 and quantity 3, but I'm sure you'd define that in some way - maybe first one wins, or last one wins, or build a list of all the values, whatever makes sense.) You could end up with something like: { "fruit": "apple", "tree": "Apple tree", "quantity": ... } depending on how you resolve the conflict. Python is an excellent language for text processing; you have a wide variety of pretty cool tools available. ChrisA