python 判断字符串是否为(或包含)IP地址
下面是某个字符串是否为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地址的更多相关文章
- 在shell中如何判断字符串是否为有效的IP地址【转】
转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...
- python判断字符串中是否包含子字符串
python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1: print('存在') else: print('不存在' ...
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- python判断字符串是否为空的方法s.strip()=='' if not s.strip():
python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='': print 's is null' 或者 if not s.strip(): p ...
- python 判断字符串中是否只有中文字符
python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...
- Python判断字符串编码以及编码的转换
转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...
- python判断字符串是否是json格式方法分享
python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例: ...
- 用最基本的遍历来实现判断字符串 a 是否被包含在字符串 b 中,并返回第一次出现的位置(找不到返回 -1)
用最基本的遍历来实现判断字符串 a 是否被包含在字符串 b 中,并返回第一次出现的位置(找不到返回 -1) 例子: a='12';b='1234567'; // 返回 0 a='47';b='1234 ...
- 华为oj-判断输入的字符串是不是一个有效的IP地址
题目标题: 判断输入的字符串是不是一个有效的IP地址 详细描述: 请实现如下接口 boolisIPAddressValid(constchar* pszIPAddr) 输入:pszIPAddr 字符串 ...
随机推荐
- 51nod--1134 最长递增子序列 (动态规划)
题目: 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行: ...
- robot总结
1 搭建环境地址 http://www.cnblogs.com/yufeihlf/p/5945102.html 2 页面描述 https://www.cnblogs.com/yufeihlf/p/59 ...
- 《剑指offer》复杂链表的复制
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- C语言对字符串去重
# include <stdio.h> # include <string.h> char * getNewChar(char * str,char * newStr); in ...
- oracle dmp文件导出与导入
ORACLE 10g导入 ORACLE 11g 一.expdp.sh导出dmp文件export PATH=$PATH:$HOME/binexport ORACLE_BASE=/oracleexport ...
- 经典合集 - WP8.1数据源
[内容说明:] 这里是一个页面类型的数据源,在开发WP应用程序中,因为目前没有数据库服务器等网络资源,突发奇想,通过博客园提供的WCF接口访问该博文地址获取文章内容,在应用程序中使用正则表达式解析并转 ...
- scrapy相关:splash 实践
0. 1.参考 https://github.com/scrapy-plugins/scrapy-splash#configuration 以此为准 scrapy相关:splash安装 A javas ...
- James Munkres Topology: Sec 18 Exer 12
Theorem 18.4 in James Munkres “Topology” states that if a function \(f : A \rightarrow X \times Y\) ...
- 遍历文件路径python版,java版
python: # 获取所有txt路径列表 file_list = [] def gci(filepath): files=os.listdir(filepath) for fi in files: ...
- python可视化pyecharts
python可视化pyecharts 简单介绍 pyecharts 是一个用于生成 Echarts 图表的类库.Echarts 是百度开源的一个数据可视化 JS 库.用 Echarts 生成的图可视化 ...