regexp??

MIB's icon

regexp always gives me headaches...

So I have filenames like C#3 or G#4. I would like to convert this into a 3 element list so I can do comparisons. For this I thought I could use [regexp \w]. Unfortunately, that gets rid of the #. Is there a quick workaround? Am I misunderstanding?

Thanks

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

Luke Hall's icon

The problem is that # isn't a word character and won't be recognised by \w. Try using [regexp .] instead which will output any character. Or something like [regexp (.) @substitute "%1 "] and see what comes out of the first outlet. This basically puts a space after every character in the input string. It should do what you want but seeing as I don't know exaclty what sort of messages you're sending I can't tell if you might run into problems using this approach.

lh

MIB's icon

that was exactly what I was looking for. Thanks!
one of these days I really have to learn how to use regexp properly.

mzed's icon

MIB wrote on Mon, 27 April 2009 13:57regexp always gives me headaches...

So I have filenames like C#3 or G#4. I would like to convert this into a 3 element list so I can do comparisons.

There's a good example of how to do this, by John MacCallum, in the CNMAT MMJ Depot. Find the whole thing here:

Then find pitch2midi.maxhelp in /CNMAT_MMJ-Depot/modules/MIDI/ Just what you want.

mz

Tj Shredder's icon

I am trapped, and can't access the CNMAT Site. I registered probably long time ago, but the e-mail address back than has vanished. I could of course create a new account as Donald Duck, but I'd prefer my real name...

chapelier fou's icon

Hi, I prefer not opening another topic...

I try to do something quite simple, but all this PCRE stuff is so weird to me.
So what I want to achieve is to process a file path like

"Macintosh HD:/Users/louiswarynski/Documents/Max/Max Patches Louis/MLR Video/Chapelier Monome Videos/Serpents/Serpent 16 onde.mp4"

to output

"Serpents/Serpent 16 onde.mp4"

So in other terms, everything after the penultimate "/"

Any help ?

Any resource to learn this stuff one and for all ?

sousastep's icon

this site is a good way to test regex. the Quick Reference is quite nice: https://regex101.com/

From claude I got

[^/]+/[^/]+$

Then plugging that into regex101 & fixing errors I get

[^\/]+\/[^\/]+$

Then plugging that into max's regexp object makes the \ disappear so we need double \ for

regexp [^\\/]+\\/[^\\/]+$

which works like so


TFL's icon

In regexp you could do .+/(.+/.+) which will capture whatever that is before and after the last /, assuming there is another / before that part to capture, itself preceded by some other character.

. stands for any character

+ stands for "one or more occurrences" of the previous character (here .)

() defines the group to capture.

/ designates the actual / in the string.

I learnt this by reading countless times the examples provided in the [regexp] help file (which lacks clarity IMO) and by watching some video tutorials about regular expression (there are a lot of them).

I also provided another way of doing this based on the powerful [fromsymbol @separator /]:

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

Roman Thilenius's icon

for path -> "Folder/Filename.mp3" i´d go the atoi/itoa route, like i did for 110.stripfile (opposite of strippath)

Kamal.h's icon

a more expensive way without regexp

penultimateworegexp.maxpat
Max Patch

chapelier fou's icon

You are all incredibly nice people !

TFL's icon

For the sake of it, here's a benchmark of all suggested methods. Interesting results:

The fastest is fromsymbol + zl

closely followed by regexp .+/(.+/.+)

closely followed by strippath + atoi + zl (I slightly improved Kamal.H solution)

and regexp [^\\/]+\\/[^\\/]+$ ends up about 3 times slower than all above solutions.

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

A weird thing is that results are slightly different if we trigger all benchmarks from the same big bang button, or one after the other. All at once, we get the results above. One after the other, strippath becomes the fastest, very close to fromsymbol, .+ regexp slightly behind and the other regexp always x3 slower.

Anyway, we're speaking about duration between 0.003 and 0.01 seconds for 10000 executions, so no real "wrong" choice here.

One thing worth mentioning is that [strippath] is always executed in the event thread contrarily to other solutions which can work at interrupt level.