MATLAB: Regexp file path directory help

regexp

I’m having issues with a regular expression. I have a file that looked in the same directory for a xml file and would see if it had a .xml extension and if it didn’t it would append it. So I had a regexp like:
XML_FileName = 'RandomFile.xml';
stringPresent = strfind(XML_FileName, '.');
if ~isempty(stringPresent)
stringParts = regexp(XML_FileName, '.', 'split');
if ~isequal(stringParts{end}, 'xml')
error('Not and XML file');
end
end
But I had someone try this:
XML_FileName = '..\NewFolder\RandomFile.xml';
And this didn’t work. I can’t figure out the regular expression to get this working. I tried:
stringParts = regexp(idfFileName, '[*\.*\\\w+\d+]\.[*\w]', 'split')
which I thought was, look for zero or more . and \ with one or more characters and number with a . and a few characters at the end. This didn’t work and variations of this didn’t work. I would like this to work if someone enters just the file name or a path location. Thanks!

Best Answer

Couldn't you just use the fileparts function for this?
[pathstr,name,ext] = fileparts(filename);
if ~strcmpi( ext, 'xml' )
error( 'Not an XML file' )
end
type of thing. I can't remember off hand whether the '.' comes with the 'xml' in the ext output or off the top of my head whether strcmpi gives a sensible answer when compared against [] but those things can be checked easily.