下面是某个字符串是否为IP地址
import re def isIP(str):
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(str):
return True
else:
return False
myStr = "10.69.36.95"
if isIP(myStr):
print(myStr,"it is a IP!")
else:
print(myStr, "it is not a IP!")
下面是某个字符串是否包含IP地址(两种方法分别进行判断)

import re
def ip_exist_two(one_url):
compile_rule = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
match_list = re.findall(compile_rule, one_url)
if match_list:
print (match_list)
else:
print('missing................')
def ip_exist_one(one_url):
compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+')
match_list = re.findall(compile_rule, one_url)
if match_list:
print( match_list)
else:
print ('missing................')
if __name__ == '__main__':
ip_list = ['http://101.23.45.67/sd/sd.html','http://www.baidu.com',
'http://34.54.65.3/dsdfjkk.htm','http://dhj.fdjjd.com/78078979/dsdfjkk.htm']
for one_url in ip_list:
ip_exist_one(one_url)
print ('****************************************************')
for one_url in ip_list:
ip_exist_two(one_url)
主要参考代码链接:https://www.jb51.net/article/141424.htm
https://www.cnblogs.com/ccz320/p/6536993.html

python 判断字符串是否为(或包含)IP地址的更多相关文章

  1. 在shell中如何判断字符串是否为有效的IP地址【转】

    转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...

  2. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  3. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  4. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  5. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  6. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

  7. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  8. 用最基本的遍历来实现判断字符串 a 是否被包含在字符串 b 中,并返回第一次出现的位置(找不到返回 -1)

    用最基本的遍历来实现判断字符串 a 是否被包含在字符串 b 中,并返回第一次出现的位置(找不到返回 -1) 例子: a='12';b='1234567'; // 返回 0 a='47';b='1234 ...

  9. 华为oj-判断输入的字符串是不是一个有效的IP地址

    题目标题: 判断输入的字符串是不是一个有效的IP地址 详细描述: 请实现如下接口 boolisIPAddressValid(constchar* pszIPAddr) 输入:pszIPAddr 字符串 ...

随机推荐

  1. JavaScript入门学习笔记(异常处理)

    try:语句测试代码块的错误,当try中的代码块出错时执行catch中的代码块. catch:语句处理错误: throw:语句创建或抛出自定义异常. 三者一起使用可以控制程序流并生成自定义异常信息. ...

  2. java---- XMLEncoder 和 XMLDecoder 和 xSteam工具使用

    XMLEncoder: 将对象写入XML数据中 import org.dom4j.DocumentException; import java.beans.XMLEncoder; import jav ...

  3. python3 基础语法(二)

    一.python3的基本数据类型: 和其他语言一样都包含了以下数据类型: 类型 含义 实例 INT 整型(integer) 1 FLOAT 浮点型 1.1 BOOL 布尔值 TRUE/FALSE ST ...

  4. php 的文件操作类

    <?php header('Content-type:text/html;charset=utf8'); Class FILE { private static $path; private s ...

  5. python selenium Chrome模拟手机浏览器

    在做移动端页面测试时可以利用Chrome mobile emulation 辅助完成页面的适配问题,但是目前手机市场上的型号居多我们也没有办法通过人工的模式一一的去适配,所以这里考虑到通过自动化的模式 ...

  6. Knockout案例: 全选

  7. [原创]基于Zynq Linux环境搭建(一)

    安装VMWare版本12 Ubuntu版本 12.04.5 64bit 系统安装完成后,登陆系统,在sotfware中心安装konsole.gvim.software source等基本软件 在sof ...

  8. vue-cli按需加载,懒加载组件

    vue来做一个单页面应用,当我们的项目越来越大,组件越来越多的时候,首次启动项目户特别慢,就算做一个加载框,蒙层之类的,体验也不会好,这个时候就需要按需加载 1.什么叫按需加载 所谓按需加载,顾名思义 ...

  9. helm-chart5,模板和访问文件

    提供的一些声明和使用命名模板段的操作: define在模板中声明一个新的命名模板 template导入一个命名模板 block 声明了一种特殊的可填写模板区域 首先,模板名称是全局的.如果声明两个具有 ...

  10. [LeetCode] Binary Trees With Factors 带因子的二叉树

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...