本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
1.简介
import re
print("匹配成功,返回一个Match对象:")
print(re.match(r"^\d{3}\-\d{3,8}$", "020-6722053"))
print("----------------------------------------------------")
print("匹配失败,返回一个None:")
print(re.match(r"^\d{3}\-\d{3,8}$", "020 6722053"))
print("----------------------------------------------------")
user_input = input("请输入测试字符串:")
if re.match(r"^W|w{1-10}", user_input):
print("It's OK.")
else:
print("Failed.")
匹配成功,返回一个Match对象:
<re.Match object; span=(0, 11), match='020-6722053'>
----------------------------------------------------
匹配失败,返回一个None:
None
----------------------------------------------------
请输入测试字符串:Willard584520
It's OK.
2.切分字符串
import re
str_input = input("Please input test string:")
print(re.split(r"\s+", str_input))
import re
str_input = input("Please input test string:")
print(re.split(r"[\s\,]+", str_input))
import re
str_input = input("Please input test string:")
print(re.split(r"[\s\,\.\;]+", str_input))
3.分组
import re
match_test = re.match(r"^(\d{3})-(\d{3,8})$","020-6722053")
print("match_test:", match_test)
print("match_group(0):", match_test.group(0))
print("match_group(1):", match_test.group(1))
print("match_group(2):", match_test.group(2))
print("---------------------------------------------------------")
website_match_test = re.match(r"(\w{3}).(\w{5}).(\w{3})", "www.baidu.com")
print("website_match_test:", website_match_test)
print("website_match_test_group(0):", website_match_test.group(0))
print("website_match_test_group(1):", website_match_test.group(1))
print("website_match_test_group(2):", website_match_test.group(2))
print("website_match_test_group(3):", website_match_test.group(3))
match_test: <re.Match object; span=(0, 11), match='020-6722053'>
match_group(0): 020-6722053
match_group(1): 020
match_group(2): 6722053
---------------------------------------------------------
website_match_test: <re.Match object; span=(0, 13), match='www.baidu.com'>
website_match_test_group(0): www.baidu.com
website_match_test_group(1): www
website_match_test_group(2): baidu
website_match_test_group(3): com
4.贪婪匹配
import re
string_input = input("Please input string:")
print("采用贪婪匹配:")
print(re.match(r"^(\d+)(0*)$", string_input).groups())
print("---------------------")
print("采用非贪婪匹配:")
print(re.match(r"^(\d+?)(0*)$", string_input).groups())
Please input string:1008600
采用贪婪匹配:
('1008600', '')
---------------------
采用非贪婪匹配:
('10086', '00')
5.编译
import re
re_telephone = re.compile(r"^(\d{3})-(\d{3,8})$")
telephone_input1 = input("Willard,please input your telphone number:")
telephone_input2 = input("Chen,Please input your telphone number:")
print("match:020-6722053,", re_telephone.match(telephone_input1).groups())
print("match:020-6722066,", re_telephone.match(telephone_input2).groups())
Willard,please input your telphone number:020-6722053
Chen,Please input your telphone number:020-6722066
match:020-6722053, ('020', '6722053')
match:020-6722066, ('020', '6722066')
|