Please explain how to understand special strings like '^(\w:)?\'

3 visualizaciones (últimos 30 días)
Ted Diehl
Ted Diehl el 13 de Oct. de 2013
Editada: Cedric el 13 de Oct. de 2013
I am trying to understand some m-files I find on this forum, but am stumped a bit by some of the strings statements used. Below are a few snippets that come from deploypcode by Sven.
Example 1:
function inPath = getFullPathStr(inPath)
% Tricky little function that returns the fully qualified path by looking for filesep
inPath = fullfile(inPath);
if ~any(regexp(inPath,['^(\w:)?\' filesep]))
inPath = fullfile(pwd, inPath);
end
end
Example 2:
mustIgnoreList = {'^\.$','^\.\.$',[mfilename '.m']};
tmp = {'\.dll$'}
My questions
  1. Why is fullfile not sufficient to get the full file path, why does Sven need to check with the following regexp function?
  2. How do I interpret '^(\w:)?\' ?
  3. In example 2, how do I interpret {'^\.$','^\.\.$',[mfilename '.m']} ? I understand he is concatenating chars, but what does the '^\.$' mean exactly?
  4. In {'\.dll$'}, what does the ending with a $ imply?
Thanks

Respuesta aceptada

Cedric
Cedric el 13 de Oct. de 2013
Editada: Cedric el 13 de Oct. de 2013
1. Look at the following:
% Windows type of absolute path.
>> fullfile( 'C:\Temp', 'C:\Temp\myFile.txt' )
ans =
C:\Temp\C:\Temp\myFile.txt
% UNIX type of absolute path.
>> fullfile( '/home/ted', '/home/ted/myFile.txt' )
ans =
\home\ted\home\ted\myFile.txt
so what is done with the regexp is simply to detect if the drive letter and ':' is already in inPath, or if the path starts with the file separator, both being cases of absolute paths.
2. In the pattern, ^ matches the start of the string, the end of the pattern \ concatenated with the file separator matches the file separator, which has to be escaped in case it's a backlash (special char. for regexp); and the middle of the pattern ()? says "as little as possible" (even 0) match of what is inside parentheses, which is one alphanumeric (or underscore but it's irrelevant here) character matched with the operator \w, followed by a semi-column. This makes the drive letter and ':' optional for managing the UNIX types of absolute paths.
3. It's a list of patterns for ignoring ., .., and the current M-file itself, I guess in a list of file names returned by the DIR function. This could be done using STRCMP instead of REGEXP. As the '.' has a special meaning in regexp patterns (= any character), it has to be escaped.
4. In regexp patterns, ^ matches the beginning of the string and $ matches the end. In 3., you can see that they frame the '\. and '\.\., which limits positive matches to only '.' and '..' and not file names which use the the . as a separator between the name and the extension, or any ill defined file name which contains '..'.
  2 comentarios
Ted Diehl
Ted Diehl el 13 de Oct. de 2013
Thanks Cedric!
Cedric
Cedric el 13 de Oct. de 2013
Editada: Cedric el 13 de Oct. de 2013
You're welcome! Note that I corrected a few typos after you wrote your comment.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by