regexp merge

Mistadobalina's icon
Max Patch
Copy patch and select New From Clipboard in Max.

I'm working with lilypond syntax. I want to be able to go from "sn8 16 cymr8" to "sn snbd cymr". How can I do that? I've come this far.

Peter McCulloch's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Here's one way to do it, and it should work for larger groups (e.g. three or more notes at once)

Mistadobalina's icon

Hey Peter. It looks pretty nice. But your example is not exactly what I after. I would also like to remove the spaces for the instrument names that are inbetween the brackets only. Can that be added to your version?

Max Patch
Copy patch and select New From Clipboard in Max.

I got it working in the following patch, but I'm sure it can be done more efficiently and cleverer.

Peter McCulloch's icon
Max Patch
Copy patch and select New From Clipboard in Max.

The problem I see with removing the spaces first is this:

You can see that it doesn't handle multiple items outside of brackets.

I'm remembering a polemic I read about not doing things like this as one liners in regexp ("Don't use regexp to parse HTML" IIRC), but instead doing it as a sort of position counter. This is a simplified version of that algorithm, but it works well.

`

Max Patch
Copy patch and select New From Clipboard in Max.

It's important to really torture test things like these to make sure they work with multiple scenarios. The one where this wouldn't is in the case of corrupted data (no ending bracket)

Mistadobalina's icon

Thanks Peter, this totally works. This is the first time I'm working with regexp so your help is very much appreciated.

Peter McCulloch's icon

BTW, if you have the option, it's way easier to deal with regexp problems like this inside of mxj or js, since you'll have indices, and they should both work with arbitrarily large strings.

Mistadobalina's icon

Well, I've got no experience with java or javascript so unfortunately that's not an option.