美文网首页
[code]Python常用匹配

[code]Python常用匹配

作者: PyKailyn | 来源:发表于2016-08-04 15:31 被阅读0次
# coding = utf-8

"""
字符验证
"""

import re


def re_match(string, re_str):
    """
    string是否能匹配re_str的正则表达式
    """
    re_compile = re.compile(re_str)
    result = re_compile.match(string)
    if not result:
        return False

    return True


def is_mobile(string):
    re_str = r"^([+][0-9]{2})?[0-9]{11}$"
    match = re_match(string, re_str)
    return match


def is_id_card(string):
    re_str = r"^(\d{17})([0-9]|X)$"
    match = re_match(string, re_str)
    return match


def is_int(string):
    """
    include 0(zero)
    """
    re_str = r"^-?(0|[1-9]\d*)$"
    match = re_match(string, re_str)
    return match


def is_float(string):
    re_str = r"^-?[0-9]+[.][0-9]+$"
    match = re_match(string, re_str)
    return match


def is_ZH(string):
    re_str = r"^[\u4E00-\u9FA5]+$"
    match = re_match(string, re_str)
    return match


def is_url(string):
    re_str = r"^http[s]?:\/\/.+$"
    match = re_match(string, re_str)
    return match


def is_mac_address(string):
    re_str = r"^([0-9A-F]{2})(-[0-9A-F]{2}){5}$"
    match = re_match(string, re_str)
    return match


def is_ip_address(string):
    base_re_str = r"(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))"
    re_str = "^%s(\.%s){3}$" % (base_re_str, base_re_str)
    match = re_match(string, re_str)
    return match

相关文章

  • [code]Python常用匹配

  • 栈:Stack

    1.action 2.code of python 3.应用:括号匹配

  • 2018-03-30

    python 正则 re 模块常用方法re.match #从头匹配re.search #...

  • coding

    code - 获取质数code - 两数之和匹配目标值code - 最长有效字符串code - 字符的有效匹配co...

  • 第十七章 Phton3 正则模块(标准库)

    常用特殊字符匹配内容 字符匹配: 次数匹配: Python 中使用正则的方法 match 只在整个字符串的起始位置...

  • 基础算法笔记 python和C++

    二分查找 python code 选择排序 python code c++ code 快速排序 python c++

  • python字符串和文本

    Python CookBook总结 用Shell 通配符匹配字符串 你想使用Unix Shell 中常用的通配符(...

  • Python系统运维常用库

    【Python系统运维常用库】 1、psutil是一个跨平台库(http://code.google.com/p/...

  • 正则表达式练习

    匹配邮箱 匹配电话号码 匹配本地磁盘路径,取出“C:\code\cnkiCrawl” 匹配Hello Kitty ...

  • python正则学习

    一、常用的匹配规则总结表 原文链接 相关具体应用例子,请见:Python-正则表达式 二、re库中常用方法 相关具...

网友评论

      本文标题:[code]Python常用匹配

      本文链接:https://www.haomeiwen.com/subject/kcgcsttx.html