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)
相关推荐
淼澄研学4 小时前
LangChain构建Prompt模板解决Python长尾报错实操
python·langchain·prompt
枫叶v.4 小时前
DeepSeek Harness(DSH)插件开发保姆级教程:从 0 到 1 快速写出第一个插件
python
ttwosix4 小时前
JavaEE初阶 多线程:单例模式 | 阻塞队列
java·开发语言
独立开发之道4 小时前
【Three.js教程】 图元速查:官方内置的立体图形
开发语言·javascript·ecmascript
Htr_4 小时前
Python面向对象编程:类与对象基础
开发语言·python
RD_daoyi4 小时前
谷歌改写了76%的标题:超60字符的,95%会被谷歌自己重写
大数据·服务器·开发语言·前端·搜索引擎·html
weixin_307779134 小时前
AI生成代码的隐患与治理:人工审查流程及提示词驱动的修复策略
开发语言·人工智能·算法
muddjsv5 小时前
Python 神经网络入门:用 scikit-learn 完成 MLP 分类与回归
python·神经网络·scikit-learn
Java后端的Ai之路5 小时前
09、Python组合模式
开发语言·人工智能·python·docker·组合模式