[sharing is fun] some regexp examples


    Jul 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)

    • Jul 22 2006 | 6:10 pm
    • Aug 05 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
    • Sep 06 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?
    • Sep 06 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.
    • Sep 07 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
    • Sep 07 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.
    • Sep 07 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.
    • Sep 08 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.
    • Sep 08 2010 | 6:15 am
      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
    • Sep 08 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.