Returning Mapped Directory from a specific subst drive

I am trying to return the real directory of a specific substituted I: drive.
%create virtual drives of the current directory, as an example
dos('subst h: /d');
dos('subst h: .');
dos('subst i: /d');
dos('subst i: .');
dos('subst j: /d');
dos('subst j: .');
%Use the dos subst command with no arguments to return cmdout
[status,cmdout]=dos('subst');
% Use the regexp command to return the directory of the I: drive.
MapDir = regexp(cmdout,'(?<=[I:\: => ]).*(?=[/r])','match')
The cmdout variable returns a string that has /n line to differentiate the different mapped drives. I am trying to pull the mapping of the i: drive out with the regexp LookAhead and LookBehind features.
How am I using the LookAhead and LookBehind features of regexp wrong?

Réponses (2)

[I:\: => ]
means any of the characters space, ':', '=', '>', or 'I', with a redundant space and a redundant ':' because ':' is not a special character for regexp purposes so '\:' is the same as ':'
If you are trying to match a literal '[' then you need to escape it, '\[' . If you are trying to match a literal '\' then you need to escape that: '\\' . Perhaps your expression should look like
'\[I:\\: => ]'
I'm not sure why you introduced brackets [] in your regular expression. That is one of the issue. A bracket expression means match any one character within the bracket, not match that string.
The second issue is that backlashes \ need to be escaped. So it should be \\.
Also, it's \r not /r. In any case, the end of line character used by subst is \n ( char(10) not char(13)). Note that you can tell the regexp to not match \n with ., so the regular expression could simply be:
MapDir = regexp(cmdout, '(?<=I:\\: => ).*', 'match', 'dotexceptnewline')

1 commentaire

Also note, that if you really wanted to use a look-ahead to find the end of the line instead of 'dotexceptnewline', you also need to look for the end of the string. If the I: drive is the last in the list returned by subst the line won't end with a '\n'. So:
MapDir = regexp(cmdout, '(?<=I:\\: => ).*?(?=\n|$)', 'match')
Also note that in that case the * has to be non-greedy: *?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Question posée :

le 17 Août 2016

Commenté :

le 17 Août 2016

Community Treasure Hunt

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

Start Hunting!

Translated by