I need a way to seperate a file location from the file itself (or the file name, to be specific). I’m developing a web-app, and I need to be able to pull out a file’s location and the file’s name seperately in order to link to them. I.e, if I have a file at web-app/files/foo/bar.bas, I need to be able to pull of 1.) web-app/files/foo/ and 2.) bar.bas.
I’m doing right now in Groovy (this is a Grails app), but I’m told that regex is much more efficient. I can use regex in Grails without trouble, but I am not familiar with it. I’m trying to learn it anyways, but I would greatly appreciate one of the gurus around here showing me who to hack this one.
What you want is an expression that has two parts:
Match everything up to and including the last /. So something like
anychar anynumberoftimes /
Everything else behind it
anychar anynumberoftimes
Those two parts will be your directory and file respectively.
There are a couple of corner cases you should take care of.
No / in path.
Nothing after the last /.
Regexes in languages these days are pretty much alike, so I’m sure you can find the documentation for Groovy regexes which will probably be similar to Java regexes or Perl regexes.
There are lots of tutes on the net about regexes, and of course there is an O’Reilly book on it.
The shell use commands like ‘dirname’ and ‘basename’. Most programming languages have functions to separate file and pathnames. They are normaly OS independant (not all), which means they would parse names correctly, whether the files are on a Unix or on a Windows servers. If you’re writing a web-apps, you should use such functions and NOT regexp. Read PHP, Perl, Python, java documentation … or whatever programming language you’re using.