本文介绍: 【代码】python 正则表达式。

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

import re

s = "hello world"
result = re.match("hello",s)
print(result)
print(result.span())
print(result.group())
s = "good hello world"
result = re.search("hello",s)
print(result)


s = "good hello world  hello earth"
result = re.findall("hello",s)
print(result)

s = "aaajkjl dja123jkjhj#hjkhd3444@"
result = re.findall(r'd',s)#r 表示是正常子符
print(result)
result = re.findall(r'W',s)# 特殊字符
print(result)

result = re.findall(r'[a-zA-Z 2-3]',s)
print(result)

#^ 开头 结尾
r = '^[0-9a-zA-Z]{6,10}$'
#满足条件
s = "123456yys"
result = re.findall(r,s)
print(result)
#不满足条件
s = "123456yys*"
result = re.findall(r,s)
print(result)


r = '^[1-9][0-9]{4,10}$'
#满足条件
s = "35878986"
result = re.findall(r,s)
print(result)
#不满足条件
s = "035878986"
result = re.findall(r,s)
print(result)

r = r'(^[w-]+(.[w-]*)*@(qq|163|gmail)(.[w-]*)+$)'
s = "a.b.e.f.g@qq.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

s = "a.b.e.f.g@123.com.e.c"
result = re.findall(r,s)
print(result)
result = re.match(r,s)
print(result)

原文地址:https://blog.csdn.net/weixin_38432324/article/details/135624780

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_57328.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注