substitute multiple numbers

lekirst's icon

Is it possible to substitute multiple numbers?
So if the combination of 5 6 is found substitute for 5?

lekirst's icon

Found it!
regexp (5\s6) @substitute 5

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

I am using the regexp to substitute numbers. However I am now facing some troubles with number with multiple digits.
How do I address the numbers above 9???

Jeremy's icon

Try this. You don't want to use character classes, instead use OR (|) matching.

I'd prefer some kind of Max-native solution here, since using regular expressions like this seems messy, but I can't think of a better way.

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

Best, Jeremy

lekirst's icon

the patch you wrote only seems to work with an input message of 3 (or 6 numbers)
how can I alter it, that also the messages with two number work?

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

a combination of both does the trick:

Jeremy's icon

That doesn't really look right to me. In fact, your pattern will substitute more than 3 numbers: 5 7 8 11 12 becomes 3.

You could try this: regexp (5|6|7|8|11)\s(6|7|8|11)((\s(6|7|8|11|12))?) @substitute 3 which uses the ? operator to make the 3rd group optional. But then it won't match "11 12", for instance, since it requires a 5, 6, 7, 8,or 11 at the front of the potential list followed by a 6, 7, 8 or 11. So that regexp will match a minimum of 2 and a maximum of 3 numbers in a row.

Regular expressions can do nearly anything related to text processing, in fact, but like any other programming, you have to be really specific about the outcome you desire.

Best, Jeremy