如果URL中存在IP地址,则返回其他内容 [英] if IP address exist in URL return something else return something

查看:167
本文介绍了如果URL中存在IP地址,则返回其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Matlab检查URL中是否存在IP地址?有什么功能可以用来检查IP地址吗?

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")

推荐答案

使用regexp,您可以使用'tokens'或具有常规匹配功能的先行和后行.这是先行/后行方法:

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

'tokens'方法具有更简单的模式,但是由于具有嵌套单元格,因此输出更加复杂:

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'

但是,相同的hasIP计算有效.

The same hasIP computation works, however.

这篇关于如果URL中存在IP地址,则返回其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆