Extract the Transport names from the list of adapter names
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
chrisw23
2022-7-27
This can be done with String operations. Look for extract extractBefore extractAfter and Pattern options.
采纳的回答
dpb
2022-7-27
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 个评论
chrisw23
2022-8-16
[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
2022-8-16
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
更多回答(1 个)
Stephen23
2022-8-16
编辑:Stephen23
2022-8-16
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(:)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!