Python正则表达式校验篇(一)---校验数字的表达式

  大家好啊!我是NiJiMingCheng

我的博客:NiJiMingCheng

这节课我们来分享Python正则表达式校验中--校验数字的表达式内容,我们结合不同的示范例子来进行匹配,获取我们需要校验的内容,并返回True或False,接下来就让我们进入学习吧,加油哦!!!

         在日常的编程和数据处理中,正则表达式是一个非常强大的工具,它可以帮助我们快速准确地匹配、校验各种格式的文本。今天,就让我们一起来详细了解一些常见的正则表达式校验示例吧!

目录

1. 校验数字:^[0-9]*$

2. n 位的数字:^\d{n}$

3. 至少 n 位的数字:^\d{n,}$

4. m - n 位的数字:^\d{m,n}$

5. 零和非零开头的数字:^(0|[1-9][0-9]*)$

6. 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$

7. 带 1 - 2 位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$

8. 正数、负数、和小数:^([-+])?\d+(\.\d+)?$

9. 有两位小数的正实数:^[0-9]+(.[0-9]{2})?$

10. 有 1 ~ 3 位小数的正实数:^[0-9]+(.[0-9]{1,3})?$

11. 非零的正整数:^[1-9]\d*$

12. 非零的负整数:^-[1-9]\d*$

13. 非负整数:^\d+$

14. 非正整数:^-([1-9]\d*|0)$

15. 非负浮点数:^\d+(\.\d+)?$


1. 校验数字:^[0-9]*$

        这个正则表达式用于校验输入的字符串是否只包含数字(可以为空字符串)。

  示例代码

import re

def validate_number(input_str):
    pattern = re.compile(r"^[0-9]*$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "123"
text2 = "你好"
print(f"校验数字:'{text1}' 是否匹配:{validate_number(text1)}")
print(f"校验数字:'{text2}' 是否匹配:{validate_number(text2)}")

   输出结果

校验数字:'123' 是否匹配:True
校验数字:'你好' 是否匹配:False

2. n 位的数字:^\d{n}$

    这里的n是指定的数字位数,该正则表达式用于校验输入的字符串是否恰好为n位数字。

示例代码

import re

def validate_n_digit_number(input_str, n):
    pattern = re.compile(r"^\d{" + str(n) + "}$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "12345"
text2 = "123"
n = 5
print(f"校验{n}位数字:'{text1}' 是否匹配:{validate_n_digit_number(text1, n)}")
print(f"校验{n}位数字:'{text2}' 是否匹配:{validate_n_digit_number(text2, n)}")

输出结果

校验5位数字:'12345' 是否匹配:True
校验5位数字:'123' 是否匹配:False

3. 至少 n 位的数字:^\d{n,}$

        此正则表达式用于校验输入的字符串是否至少包含n位数字。

示例代码

import re

def validate_at_least_n_digit_number(input_str, n):
    pattern = re.compile(r"^\d{" + str(n) + ",}$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "12345"
text2 = "123"
text3 = "12"
n = 3
print(f"校验至少{n}位数字:'{text1}' 是否匹配:{validate_at_least_n_digit_number(text1, n)}")
print(f"校验至少{n}位数字:'{text2}' 是否匹配:{validate_at_least_n_digit_number(text2, n)}")
print(f"校验至少{n}位数字:'{text3}' 是否匹配:{validate_at_least_n_digit_number(text3, n)}")

输出结果

校验至少3位数字:'12345' 是否匹配:True
校验至少3位数字:'123' 是否匹配:True
校验至少3位数字:'12' 是否匹配:False

4. m - n 位的数字:^\d{m,n}$

    用于校验输入的字符串的数字位数是否在mn之间(包含mn)。

示例代码

import re

def validate_m_n_digit_number(input_str, m, n):
    pattern = re.compile(r"^\d{" + str(m) + "," + str(n) + "}$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "1234"
text2 = "123456"
text3 = "12"
m = 3
n = 5
print(f"校验{m} - {n}位数字:'{text1}' 是否匹配:{validate_m_n_digit_number(text1, m, n)}")
print(f"校验{m} - {n}位数字:'{text2}' 是否匹配:{validate_m_n_digit_number(text2, m, n)}")
print(f"校验{m} - {n}位数字:'{text3}' 是否匹配:{validate_m_n_digit_number(text3, m, n)}")

输出结果

校验3 - 5位数字:'1234' 是否匹配:True
校验3 - 5位数字:'123456' 是否匹配:False
校验3 - 5位数字:'12' 是否匹配:False

5. 零和非零开头的数字:^(0|[1-9][0-9]*)$

     校验输入的字符串是否是以零开头或者以非零数字开头的数字。

示例代码

import re

def validate_zero_or_non_zero_start_number(input_str):
    pattern = re.compile(r"^(0|[1-9][0-9]*)$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "0"
text2 = "123"
text3 = "0123"
print(f"校验零和非零开头的数字:'{text1}' 是否匹配:{validate_zero_or_non_zero_start_number(text1)}")
print(f"校验零和非零开头的数字:'{text2}' 是否匹配:{validate_zero_or_non_zero_start_number(text2)}")
print(f"校验零和非零开头的数字:'{text3}' 是否匹配:{validate_zero_or_non_zero_start_number(text3)}")

输出结果

校验零和非零开头的数字:'0' 是否匹配:True
校验零和非零开头的数字:'123' 是否匹配:True
校验零和非零开头的数字:'0123' 是否匹配:True

6. 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$

    用于校验输入的字符串是否是以非零数字开头,并且最多带有两位小数的数字。

示例代码

import re

def validate_non_zero_start_with_two_decimal_number(input_str):
    pattern = re.compile(r"^([1-9][0-9]*)+(.[0-9]{1,2})?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "123.45"
text2 = "123"
text3 = "0.12"
text4 = "123.456"
print(f"校验非零开头的最多带两位小数的数字:'{text1}' 是否匹配:{validate_non_zero_start_with_two_decimal_number(text1)}")
print(f"校验非零开头的最多带两位小数的数字:'{text2}' 是否匹配:{validate_non_zero_start_with_two_decimal_number(text2)}")
print(f"校验非零开头的最多带两位小数的数字:'{text3}' 是否匹配:{validate_non_zero_start_with_two_decimal_number(text3)}")
print(f"校验非零开头的最多带两位小数的数字:'{text4}' 是否匹配:{validate_non_zero_start_with_two_decimal_number(text4)}")

输出结果

校验非零开头的最多带两位小数的数字:'123.45' 是否匹配:True
校验非零开头的最多带两位小数的数字:'123' 是否匹配:True
校验非零开头的最多带两位小数的数字:'0.12' 是否匹配:False
校验非零开头的最多带两位小数的数字:'123.456' 是否匹配:False

7. 带 1 - 2 位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$

    校验输入的字符串是否是带 1 - 2 位小数的正数或负数。

示例代码

import re

def validate_number_with_one_two_decimal_positive_negative(input_str):
    pattern = re.compile(r"^(\-)?\d+(\.\d{1,2})?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "-12.34"
text2 = "12.3"
text3 = "123"
print(f"校验带1 - 2位小数的正数或负数:'{text1}' 是否匹配:{validate_number_with_one_two_decimal_positive_negative(text1)}")
print(f"校验带1 - 2位小数的正数或负数:'{text2}' 是否匹配:{validate_number_with_one_two_decimal_positive_negative(text2)}")
print(f"校验带1 - 2位小数的正数或负数:'{text3}' 是否匹配:{validate_number_with_one_two_decimal_positive_negative(text3)}")

输出结果

校验带1 - 2位小数的正数或负数:'-12.34' 是否匹配:True
校验带1 - 2位小数的正数或负数:'12.3' 是否匹配:True
校验带1 - 2位小数的正数或负数:'123' 是否匹配:False

8. 正数、负数、和小数:^([-+])?\d+(\.\d+)?$

    校验输入的字符串是否是正数、负数或者小数。

示例代码

import re

def validate_positive_negative_decimal(input_str):
    pattern = re.compile(r"^([-+])?\d+(\.\d+)?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "-12.3"
text2 = "12.3"
text3 = "123"
text4 = "+123"
print(f"校验正数、负数、和小数:'{text1}' 是否匹配:{validate_positive_negative_decimal(text1)}")
print(f"校验正数、负数、和小数:'{text2}' 是否匹配:{validate_positive_negative_decimal(text2)}")
print(f"校验正数、负数、和小数:'{text3}' 是否匹配:{validate_positive_negative_decimal(text3)}")
print(f"校验正数、负数、和小数:' +123' 是否匹配:{validate_positive_negative_decimal(text4)}")

输出结果

校验正数、负数、和小数:'-12.3' 是否匹配:True
校验正数、负数、和小数:'12.3' 是否匹配:True
校验正数、负数、和小数:'123' 是否匹配:True
校验正数、负数、和小数:'+123' 是否匹配:True

9. 有两位小数的正实数:^[0-9]+(.[0-9]{2})?$

   校验输入的字符串是否是有两位小数的正实数。

示例代码

import re

def validate_positive_real_with_two_decimal(input_str):
    pattern = re.compile(r"^[0-9]+(.[0-9]{2})?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "12.34"
text2 = "123"
print(f"校验有两位小数的正实数:'{text1}' 是否匹配:{validate_positive_real_with_two_decimal(text1)}")
print(f"校验有两位小数的正实数:'{text2}' 是否匹配:{validate_positive_real_with_two_decimal(text2)}")

输出结果

校验有两位小数的正实数:'12.34' 是否匹配:True
校验有两位小数的正实数:'123' 是否匹配:False

10. 有 1 ~ 3 位小数的正实数:^[0-9]+(.[0-9]{1,3})?$

   校验输入的字符串是否是有 1 ~ 3 位小数的正实数。

示例代码

import re

def validate_positive_real_with_one_three_decimal(input_str):
    pattern = re.compile(r"^[0-9]+(.[0-9]{1,3})?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "12.345"
text2 = "123"
print(f"校验有1 ~ 3位小数的正实数:'{text1}' 是否匹配:{validate_positive_real_with_one_three_decimal(text1)}")
print(f"校验有1 ~ 3位小数的正实数:'123' 是否匹配:False

输出结果

校验有1 ~ 3位小数的正实数:'12.345' 是否匹配:True
校验有1 ~ 3位小数的正实数:'123' 是否匹配:False

11. 非零的正整数:^[1-9]\d*$

   校验输入的字符串是否是非零的正整数。

示例代码

import re

def validate_non_zero_positive_integer(input_str):
    pattern = re.compile(r"^[1-9]\d*$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "123"
text2 = "0"
print(f"校验非零的正整数:'{text1}' 是否匹配:{validate_non_zero_positive_integer(text1)}")
print(f"校验非零的正整数:'{text2}' 是否匹配:{validate_non_zero_positive_integer(text2)}")

输出结果

校验非零的正整数:'123' 是否匹配:True
校验非零的正整数:'0' 是否匹配:False

12. 非零的负整数:^-[1-9]\d*$

   校验输入的字符串是否是非零的负整数。

示例代码

import re

def validate_non_zero_negative_integer(input_str):
    pattern = re.compile(r"^-[1-9]\d*$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "-123"
text2 = "0"
print(f"校验非零的负整数:'{text1}' 是否匹配:{validate_non_zero_negative_integer(text1)}")
print(f"校验非零的负整数:'{text2}' 是否匹配:{validate_non_zero_negative_integer(text2)}")

输出结果

校验非零的负整数:'-123' 是否匹配:True
校验非零的负整数:'0' 是否匹配:False

13. 非负整数:^\d+$

   校验输入的字符串是否是非负整数。

示例代码

import re

def validate_non_negative_integer(input_str):
    pattern = re.compile(r"^\d+$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "123"
text2 = "0"
print(f"校验非负整数:'{text1}' 是否匹配:{validate_non_negative_integer(text1)}")
print(f"校验非负整数:'{text2}' 是否匹配:{validate_non_negative_integer(text2)}")

输出结果

校验非负整数:'123' 是否匹配:True
校验非负整数:'0' 是否匹配:True

14. 非正整数:^-([1-9]\d*|0)$

   校验输入的字符串是否是非正整数。

示例代码

import re


def validate_non_positive_integer(input_str):
    pattern = re.compile(r"^-([1-9]\d*|0)$")
    if re.match(pattern, input_str):
        return True
    return False


# 示例匹配文本
text1 = "-123"
text2 = "0"
text3 = '12.23'
print(f"校验非正整数:'{text1}' 是否匹配:{validate_non_positive_integer(text1)}")
print(f"校验非正整数:'{text2}' 是否匹配:{validate_non_positive_integer(text2)}")
print(f"校验非正整数:'{text3}' 是否匹配:{validate_non_positive_integer(text3)}")

输出结果

校验非正整数:'-123' 是否匹配:True
校验非正整数:'0' 是否匹配:True
校验非正整数:'12.23' 是否匹配:False

15. 非负浮点数:^\d+(\.\d+)?$

    校验输入的字符串是否是非负浮点数。

示例代码

import re

def validate_non_negative_floating_point(input_str):
    pattern = re.compile(r"^\d+(\.\d+)?$")
    if re.match(pattern, input_str):
        return True
    return False

# 示例匹配文本
text1 = "12.3"
text2 = "123"
text3 = "0"
print(f"校验非负浮点数:'{text1}' 是否匹配:{validate_non_negative_floating_point(text1)}")
print(f"校验非负浮点数:'{text2}' 是否匹配:{validate_non_negative_floating_point(text2)}")
print(f"校验非负浮点数:'{text3}' 是否匹配:{validate_non_negative_floating_point(text3)}")

输出结果

校验非负浮点数:'12.3' 是否匹配:True
校验非负浮点数:'123' 是否匹配:True
校验非负浮点数:'0' 是否匹配:True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值