regexp file path directory help

15 views (last 30 days)
Sasquatch
Sasquatch on 16 Dec 2014
Commented: Guillaume on 16 Dec 2014
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!
  1 Comment
Guillaume
Guillaume on 16 Dec 2014
For the record, your expression does not make much sense. In particular the square brackets are used to define character classes and everything inside is interpreted as literal character.
What your expression would match I think (it looks very weird) is a '*' or a '.' or a '\' or a 'w' or a '+' or a 'd' followed by a '.' followed by '*' or 'w'.
Even if you'd used the round brackets, your expression still wouldn't work. For a start, the 0 or more operator '*' needs to be after the expression you want to repeat.
Using fileparts as Adam answered is the best solution. However, for reference, a simple regex to extract the extension would be:
ext = regexp(filename, '(?<=\.)[^.]*$', 'match')
which means: look before for a '.' and match everything but a '.' up to the end of the string.

Sign in to comment.

Accepted Answer

Adam
Adam on 16 Dec 2014
Edited: Adam on 16 Dec 2014
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.
  1 Comment
Sasquatch
Sasquatch on 16 Dec 2014
>< I was even using this in the file as well! Thanks!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!