Extract the Transport names from the list of adapter names

4 Ansichten (letzte 30 Tage)
Hello,
I am trying to create a list from the following 1*554 char array that gets created in Maltab after doing "[g, f] = system('getmac)'"
'
Physical Address Transport Name
=================== ==========================================================
08-6A-C5-47-AD-F1 \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}
08-6A-C5-47-AD-F5 Media disconnected
E0-70-EA-F2-80-43 Media disconnected
02-50-41-00-00-01 \Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}
0A-00-27-00-00-2D \Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}
'
Now my question is how do I build a list of only the adapter names(Transport names) that starts with \Device and ends with }
so basically want to create a list/variable in matlab as follows that I can then list as user options into my GUI as a drop down menu.
list = \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}
\Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}
\Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}
I am having troubles to come up with a regexp pattern for this. Can anyone provide some hints ?
  1 Kommentar
chrisw23
chrisw23 am 27 Jul. 2022
This can be done with String operations. Look for extract extractBefore extractAfter and Pattern options.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 27 Jul. 2022
I was going after @chrisw23's mode as I have a terrible time with regexp -- I can find both the start and the end of the string, but I cannot get it to return the token if I put both together in one expression similar to the example in the doc. I've no klew what's wrong with it nor what to do to try to fix it so I do as per usual -- get frustrated-enough to go at it somehow else.
This one isn't too bad -- I'm sure a whizard could do it in a one-liner with regexp, but
f=strtrim(split(f,newline))
f=strtrim(split(f(contains(f,'\Device'))))
list=f(:,2)
seems to do the trick ok. Or, you could use the new-fangled strings if cared to.
I'm frustrated with extractXXX and friends -- there isn't an extractFrom to pull at the matching string; only before or after the match--here and so often it leaves you without the beginning portion one would like. On occasion I've resorted to inserting a code character into the string first, but that's a hack. Otherwise, one has to use another strfind operation to locate the character position and extract by position.
regexp would undoubtedly still be a more elegant solution, but it'll take a better guru than I...or at least am willing to spend any further time over; any lesson I learned I'll have forgotten by the next time wanted it.
  2 Kommentare
chrisw23
chrisw23 am 16 Aug. 2022
[g, f] = system('getmac');
macListOut = string(f).split(newline);
macId = macListOut.startsWith(digitsPattern);
splitPat = alphanumericsPattern(2) + " ";
macDescription = macListOut(macId).extractAfter(splitPat).strip();
macAddress = macListOut(macId).extractBefore(macDescription).strip();
[macAddress macDescription]
..just an example..no oneliner :)
chrisw23
chrisw23 am 16 Aug. 2022
import System.Net.NetworkInformation.*
nics = NetworkInterface.GetAllNetworkInterfaces();
enNics = nics.GetEnumerator();
while enNics.MoveNext
curNic = enNics.Current;
details(curNic) % select what you need (see available properties and methods)
% example data struct
iName = matlab.lang.makeValidName(string(curNic.Name));
Intf.(iName).Description = string(curNic.Description.ToString());
Intf.(iName).OpState = curNic.OperationalStatus;
Intf.(iName).PhysicalAddress = string(curNic.GetPhysicalAddress.ToString());
Intf.(iName).IsAvailable = string(curNic.GetIsNetworkAvailable);
end
beside the RegExp/String topic......modify this to get all information you need about NetworkInterfaces on your system

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 16 Aug. 2022
Bearbeitet: Stephen23 am 16 Aug. 2022
Oneliner :)
txt = 'Physical Address Transport Name =================== ========================================================== 08-6A-C5-47-AD-F1 \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29} 08-6A-C5-47-AD-F5 Media disconnected E0-70-EA-F2-80-43 Media disconnected 02-50-41-00-00-01 \Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37} 0A-00-27-00-00-2D \Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967} ';
out = regexp(txt,'\\Device[^}]+}','match');
out(:)
ans = 3×1 cell array
{'\Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}'} {'\Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}'} {'\Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}'}

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by