Forums > MaxMSP

[sharing is fun] some regexp examples

July 22, 2006 | 5:28 pm

regexp is incredibly useful, but to the newbie, it tends to look more like cartoon cursing than logical manipulation. Here’s some useful regexp’s that I’ve figured out, including a comment on using regexp in javascript, which you should be able to extend to other needs.

Here’s a menu of the functions:
parse extension
only directory path
directory name (like strippath for a folder)
force extension
get hard drive name
strip path
strip trailing slash
get standalone application location
remove last directory (go back)

#P window setfont Geneva 9.;
#P window linecount 14;
#P comment 29 619 261 196617 function dirname(path) { var tosplit = path ; var reg1 = new RegExp("(.*)(\\/.*)") ; var noslash = tosplit.split(reg1) ; //post(noslash[0] , "–" , noslash[1] , "–" , noslash[2] , "n") ; var reg2 = new RegExp("(.*\\/)(.*$)") ; var dirs = noslash[1].split(reg2) ; //post(dirs[0] , "–" , dirs[1] , "–" , dirs[2] , "n") ; //post("name" , dirs[2] , "n") ; return dirs[2] ; };
#P window linecount 1;
#P comment 28 600 198 196617 Here’s a Javascript version of the above:;
#P window setfont "Sans Serif" 9.;
#P comment 322 205 187 131137545 FORCE EXTENSION;
#P comment 561 204 187 131137545 GET STANDALONE APP LOCATION NAME;
#P newex 622 252 47 131137545 loadbang;
#P comment 23 173 134 131137545 ONLY THE DIRECTORY PATH;
#P comment 37 25 100 131137545 PARSE EXTENSION;
#P window linecount 3;
#P message 427 33 138 131137545 Harddrive:/Sou!nds/Assorted Samples. 3 /Harp/003Harp Strums .3.mp3;
#P window setfont Geneva 9.;
#P window linecount 1;
#P comment 39 372 147 196617 DIRECTORY NAME;
#P comment 28 569 147 196617 note: must have trailing slash!;
#P comment 29 553 232 196617 like strippath for folders;
#P comment 29 538 238 196617 turns "HD:/folder1/folder2/media/" into "media";
#P window setfont "Sans Serif" 9.;
#P message 98 402 303 131137545 Harddrive:/Sou!nds/Assorted Samples. 3 /Harp/003Harp Strums .3.mp3;
#P newex 38 518 49 131137545 print path;
#P comment 600 423 100 131137545 GO BACK;
#P comment 678 515 120 131137545 strip everything after last /;
#P newex 601 539 64 131137545 print GoBack;
#P message 601 438 247 131137545 Harddrive:/Sou!nds/Assorted Samples/In yer face/;
#P newex 601 454 50 131137545 tosymbol;
#P newex 601 473 98 131137545 regexp (.*)(\\/.*);
#P comment 700 474 100 131137545 strip trailing slash;
#P newex 601 492 47 131137545 zl ecils 1;
#P newex 601 514 75 131137545 regexp (.*\\/).*$;
#P newex 59 497 47 131137545 zl ecils 1;
#P comment 126 476 120 131137545 strip everything after last /;
#P newex 104 520 86 131137545 print JustDirectory;
#P message 37 386 247 131137545 Harddrive:/Sou!nds/Assorted Samples/In yer face/;
#P newex 37 404 50 131137545 tosymbol;
#P newex 36 426 98 131137545 regexp (.*)(\\/.*);
#P comment 135 427 100 131137545 strip trailing slash;
#P newex 43 452 47 131137545 zl ecils 1;
#P newex 43 474 81 131137545 regexp (.*\\/)(.*$);
#P message 22 189 171 131137545 Eonta:/Sounds/Assorted Samples/Harp;
#P button 446 462 15 0;
#P newex 408 463 33 131137545 v app;
#P message 337 444 247 131137545 Harddrive:/Sou!nds/Assorted Samples/In yer face/;
#P newex 394 542 59 131137545 print Slash;
#P newex 335 542 55 131137545 print Path;
#P newex 360 516 51 131137545 zl ecils 1;
#P newex 337 463 50 131137545 tosymbol;
#P newex 336 485 79 131137545 regexp (.*)(:\\/.*);
#P comment 338 430 71 131137545 GET HD NAME;
#P message 613 38 247 131137545 Harddrive:/Sou!nds/Assorted Samples/In yer face/;
#P newex 670 135 59 131137545 print Slash;
#P newex 611 135 55 131137545 print Path;
#P newex 636 109 51 131137545 zl ecils 1;
#P newex 613 56 50 131137545 tosymbol;
#P newex 612 78 98 131137545 regexp (.*)(\\/.*);
#P comment 613 22 112 131137545 STRIP TRAILING SLASH;
#P newex 485 152 51 131137545 print File;
#P newex 426 152 55 131137545 print Path;
#P newex 451 126 51 131137545 zl ecils 1;
#P newex 428 73 50 131137545 tosymbol;
#P newex 427 95 98 131137545 regexp (.*\\/)(.*);
#P newex 135 138 69 131137545 print THEEXT;
#P newex 33 137 96 131137545 print NOEXTENSION;
#P message 340 237 99 131137545 my friend is mookie;
#P message 323 221 115 131137545 jones is a tweeker.mov;
#N vpatcher 172 206 452 560;
#P window setfont "Sans Serif" 9.;
#P newex 50 53 50 131137545 tosymbol;
#P newex 49 288 80 131137545 sprintf %s.mov;
#P newex 75 201 21 131137545 t 2;
#P newex 51 222 47 131137545 gate 2 1;
#P newex 50 76 40 131137545 t s s 1;
#P newex 75 179 59 131137545 match mov;
#P newex 75 152 51 131137545 zl ecils 1;
#P newex 50 123 123 131137545 regexp (.*\\.)([\\w]*);
#P inlet 50 30 15 0;
#P outlet 49 310 15 0;
#P window setfont Geneva 9.;
#P comment 111 50 100 196617 if a file has the .mov extension , it passes. if it doesn , the .mov is added;
#P connect 7 0 9 0;
#P connect 7 1 1 0;
#P connect 9 0 1 0;
#P connect 2 0 10 0;
#P connect 10 0 6 0;
#P connect 6 1 3 0;
#P connect 6 2 7 0;
#P connect 8 0 7 0;
#P connect 3 1 4 0;
#P connect 4 1 5 0;
#P connect 5 0 8 0;
#P connect 6 0 7 1;
#P pop;
#P newobj 321 258 76 131137545 p alwaysAddExt;
#P newex 321 283 77 131137545 print AUTOEXT;
#P message 572 234 219 131137545 re "(.*\\/)Application Name.app/Contents/MacOS/$";
#P message 559 216 383 131137545 Eonta:/Sounds/Assorted Samples/Harp/AppFolder/Application Name.app/Contents/MacOS/;
#P newex 559 300 69 131137545 print APPDIR;
#P newex 559 250 50 131137545 tosymbol;
#P newex 559 276 66 131137545 regexp;
#P message 34 208 267 131137545 Eonta:/Sounds/Assorted Samples/Harp/Harp Strums.aif;
#P newex 46 291 50 131137545 print DIR;
#P newex 23 242 50 131137545 tosymbol;
#P newex 23 268 95 131137545 regexp (.*\\/).*$;
#P message 36 39 305 131137545 Harddrive:/Sou!nds/Assorted Samples. 3 /Harp/003Harp Strums .3.mp3;
#P newex 58 111 51 131137545 zl ecils 1;
#P newex 35 58 50 131137545 tosymbol;
#P newex 34 80 123 131137545 regexp (.*\\.)([\\w]*);
#P comment 427 21 100 131137545 STRIP PATH;
#P connect 8 0 6 0;
#P connect 41 0 6 0;
#P connect 6 0 5 0;
#P connect 3 0 18 0;
#P connect 2 0 1 0;
#P connect 4 0 2 0;
#P connect 46 0 45 0;
#P connect 47 0 46 0;
#P connect 61 0 46 0;
#P connect 50 0 60 0;
#P connect 45 1 43 0;
#P connect 43 0 42 0;
#P connect 5 1 7 0;
#P connect 1 1 3 0;
#P connect 42 1 50 0;
#P connect 50 1 48 0;
#P connect 3 1 19 0;
#P connect 16 0 15 0;
#P connect 17 0 15 0;
#P connect 15 0 14 0;
#P connect 35 0 36 0;
#P connect 34 0 33 0;
#P connect 39 0 34 0;
#P connect 38 0 34 0;
#P connect 33 1 35 0;
#P connect 35 1 37 0;
#P connect 40 0 39 0;
#P connect 22 0 23 0;
#P connect 21 0 20 0;
#P connect 66 0 21 0;
#P connect 20 1 22 0;
#P connect 22 1 24 0;
#P connect 12 0 10 0;
#P connect 10 0 9 0;
#P connect 13 0 9 0;
#P connect 9 1 11 0;
#P connect 9 3 11 0;
#P connect 69 0 13 0;
#P connect 56 0 55 0;
#P connect 55 0 54 0;
#P connect 54 1 52 0;
#P connect 52 0 51 0;
#P connect 51 1 57 0;
#P connect 28 0 29 0;
#P connect 27 0 26 0;
#P connect 31 0 27 0;
#P connect 26 1 28 0;
#P connect 28 1 30 0;
#P window clipboard copycount 74;



jln
July 22, 2006 | 6:10 pm



RHS
August 5, 2006 | 8:28 pm

I need a Max programmer for hire can you help me? My email is rlm453@aol.com call collect 323-656-6843 Randy


September 6, 2010 | 5:18 am

These examples have been very helpful for me but I find that I am stuck trying to figure out how to adjust the parse extension example so that it also deletes the . (dot) that remains at the end of the filename. Can someone show me the way, please?


September 6, 2010 | 8:12 am

maybe something like regexp (.+)(?:\.dot)?, and take the backreference output. Otherwise post an example of what you’re trying to do.


September 7, 2010 | 4:14 am

Ok, here’s the relevant part of my patch. I’m parsing the file extension–and (hopefully, if I can figure out how) the period at the end of the filename–to send the filename to the sprintf object that combines the output of the counter with the filename to name the image that is created by the exportimage message. (Since exportimage appends a .png extension to the filename, I’ll wind up with filenames containing two consecutive dots which I really would like to avoid.)

Jonathan

– Pasted Max Patch, click to expand. –

September 7, 2010 | 4:55 pm

Try using this expression instead: [regexp (.*)\.[\w]*]

It will grab the string before the extension and ignore the period. I hope it helps.


September 7, 2010 | 8:26 pm

luke, that regexp didn’t work for me.

What I’ve always done, and I know this is a bad practice, is to do:
[regexp \. " "], which simply replaces a dot with a space. That way you can treat it as a list, and slice the filetype out, flip it around, etc.

- bad practice of course, because you may have a file called "this.has.too.many.dots.exe", but I’ve always been able to patch around that.


September 8, 2010 | 3:09 am

Unfortunately, that expression doesn’t work for me either. Seems it may be simplest thing for now is to use Automator (OS X 10.6) to change myfile..png to myfile.png. It takes less than a minute to setup and process the whole batch. Eventually, the perfect expression will emerge to eliminate the need for that extra (but simple) step.


September 8, 2010 | 6:15 am

here is another solution :

– Pasted Max Patch, click to expand. –

this one splits the file name in 2 parts (fileName + extension) (and removes the dot) ; so you can use the parsed extension for routing, for example…

Mathieu


September 8, 2010 | 3:43 pm

Matthieu, thanks. That worked fine.

Luke, I’m not sure what I did wrong yesterday but your expression also worked just fine now. Thanks.


Viewing 11 posts - 1 through 11 (of 11 total)