grab all text between two words via regexp

Jun 26, 2008 at 1:48am

grab all text between two words via regexp

After 3 hours of trying to figure this out on my own…without success…I figure it’s time to throw in towel and ask for some help!

Is there a way, using regexp, to grab the text that exists between two different key words? For example, let’s say I have the phrase:

pitch [@auto -init 0 -ramp 127 3000 -loop 0] velocity 127 pitch [@auto -init 45 -ramp 50 100 -loop 1] velocity 64

I want to grab everything that is between “pitch” and “velocity”, i.e. [@auto -init 0 -ramp 127 3000 -loop 0] and [@auto -init 45 -ramp 50 100 -loop 1]

I can do this using the following regexp:
regexp @re pitch[^(v)]*

This is fine if I have no other values that contain the letter “v”. Ideally what I’d like to do is have it parse based on the entire word, something like:
regexp @re pitch[^(velocity)]*

but I can’t figure out how to make regexp look for the entire word (in the above it treats each letter of “velocity” as an or, e.g. stop at “v” or “e” or “l” or …

I’ve tried all sorts of combinations of [], (), “”…and just can’t get it to work. Any suggestions?

Thanks!
David

#38603
Jun 26, 2008 at 2:04am

On 26 juin 08, at 03:48, David Beaudry wrote:

> After 3 hours of trying to figure this out on my own…without
> success…I figure it’s time to throw in towel and ask for some help!
>
> Is there a way, using regexp, to grab the text that exists between
> two different key words? For example, let’s say I have the phrase:
>
> pitch [@auto -init 0 -ramp 127 3000 -loop 0] velocity 127 pitch
> [@auto -init 45 -ramp 50 100 -loop 1] velocity 64
>
> I want to grab everything that is between “pitch” and “velocity”,
> i.e. [@auto -init 0 -ramp 127 3000 -loop 0] and [@auto -init 45 -
> ramp 50 100 -loop 1]
>
> I can do this using the following regexp:
> regexp @re pitch[^(v)]*
>
> This is fine if I have no other values that contain the letter “v”.
> Ideally what I’d like to do is have it parse based on the entire
> word, something like:
> regexp @re pitch[^(velocity)]*
>
> but I can’t figure out how to make regexp look for the entire word
> (in the above it treats each letter of “velocity” as an or, e.g.
> stop at “v” or “e” or “l” or …
>
> I’ve tried all sorts of combinations of [], (), “”…and just can’t
> get it to work. Any suggestions?

something like that, (negative class)

regexp @re “pitch \[[^]]+\]”

or with lazy quantifier and look forward

regexp @re pitch.+?(?=velocity)

Thanks for the fun game before going to bed;-)
ej

– Pasted Max Patch, click to expand. –
#134779
Jun 26, 2008 at 2:10am

OMG that’s just too easy!
Thank you so much for the help, ej!!

David

#134780

You must be logged in to reply to this topic.