问题
How do I check whether the IP address exist in URL by using Matlab? Is there any function that can be used to check the IP address?
data =['http://95.154.196.187/broser/6716804bc5a91f707a34479012dad47c/',
'http://95.154.196.187/broser/',
'http://paypal.com.cgi-bin-websc5.b4d80a13c0a2116480.ee0r-cmd-login-submit-dispatch-']
def IP_exist(data):
for b in data:
containsdigit = any(a.isdigit() for a in b)
if containsdigit:
print("1")
else:
print("0")
回答1:
With regexp
, you can either use 'tokens'
or look-aheads and look-behinds with regular matching. Here's the look-ahead/behind approach:
>> str = {'http://95.154.196.187/broser/6716804bc5a91f707a34479012dad47c/',
'http://95.154.196.187/broser/',
'http://paypal.com.cgi-bin-websc5.b4d80a13c0a2116480.ee0r-cmd-login-submit-dispatch-'};
>> IPs = regexp(str,'(?<=//)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=/)','match')
IPs =
{1x1 cell}
{1x1 cell}
{}
>> IPs{1}
ans =
'95.154.196.187'
>> hasIP = ~cellfun(@isempty,IPs).'
hasIP =
1 1 0
The 'tokens'
approach has a simpler pattern, but the output is more complicated as it has nested cells:
>> IPs = regexp(str,'//(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/','tokens')
IPs =
{1x1 cell}
{1x1 cell}
{}
>> IPs{1}
ans =
{1x1 cell}
>> IPs{1}{1}
ans =
'95.154.196.187'
The same hasIP
computation works, however.
来源:https://stackoverflow.com/questions/22074304/if-ip-address-exist-in-url-return-something-else-return-something