[sharing is fun] some regexp examples

Peter Nyboer's icon

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)

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


jln's icon
RHS's icon

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

jonathanb's icon

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?

Emmanuel Jourdan's icon

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

jonathanb's icon

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

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

Luke Hall's icon

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

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

Wetterberg's icon

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.

jonathanb's icon

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.

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

here is another solution :

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

jonathanb's icon

Matthieu, thanks. That worked fine.

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

Russell Richards's icon

Peter, That "Go Back" is just what I needed.

Thanks for putting up all these examples.

Russell

Roman Thilenius's icon

how would you make sure that an extension is only removed when it exists && only when it consists of 3 characters?

tried to change [regexp (.*)\\.(.*)] to [regexp (.*)\\.(...)] but that of course does not help much, it would just remove additional positions >3 behind the dot.

desired performance:
file -> file
file.aif -> file
file.002.aif -> file.002
file.aiff -> file.aiff