6.re正则表达式
2024-09-02 15:50:17
import re
import unicodedata s = "a00xoghasalexjkdfldhfjk"
v = s.find("alex")
print(v) k = ""
# 正则模糊匹配 # print(re.findall("alex", s))
# # .是通配符(除了换行之外)
# print(re.findall("a..x", s))
# # ^ (开头)和 $(结尾)
# print(re.findall("^a..x", s))
# print(re.findall("h..k$", s))
# # * (0-max) ;+(1,max) ; ? (0,1) 贪婪匹配
# b = "ccsrwrfdddddddddddkokosadkfod"
# print(re.findall("d*", b))
# print(re.findall("d+", b))
# print(re.findall("alex*", "asdhfale")) # 0-无穷大个
# print(re.findall("alex+", "asdhfale")) # 1-无穷大
# print(re.findall("alex?", "asdhfalex")) # 0-1个
# print(re.findall("alex?", "asdhfale")) # 0-1个
#
# # {}0-无穷大个 == * ; {1,} == + ; {6} 6次
# # {} 可以带贪婪匹配
# print(re.findall("alex{1,4}", "asdhfalexxx")) # 1-4个
# print(re.findall("alex{6}", "asdhfalexxx")) # 必须是6个
# # ? 惰性匹配
# print(re.findall("alex*?", "asdhfalexxx"))
# print(re.findall("alex+?", "asdhfalexxx"))
#
# # []字符集 中没有特殊符号(除了\-^)
# print(re.findall("www[oldboy baidu]", "wwwbaidu"))
# print(re.findall("x[ys]", "xyyszz"))
# print(re.findall("s[zby]a", "xyyszasya"))
# print(re.findall("s[zb,]a", "xyyszas,a"))
#
# print(re.findall("s[zb,]a", "xyyszas,a"))
# print(re.findall("s[zb*]a", "xyyscas,a"))
#
# # -之间
# print(re.findall("s[a-z]*", "sqaaaaerqwr"))
# print(re.findall("s[a-z]*", "sqaaaaerqwr9")) # [a-z] 之间
#
# print(re.findall("s[0-9]*", "s9qaaaaerqwr9")) # [a-z] 之间
# # ^非
# print(re.findall("s[^a-z]", "sqaaaaerqwr9")) # [a-z] 之间
# # \转义
# print(re.findall("\([^()]*\)", "12*(34*6+2-5*(2-1))"))
# print(re.findall("\([^()]+\)", "12*(34*6+2-5*(2-1))"))
#
# # \d 【0-9】的数值
# print(re.findall("\d+", "12*(34*6+2-5*(2-1))")) # [0-9]
# print(re.findall("\D+", "12*(34*6+2-5*(2-1))")) # [^0-9]
# print(re.findall("\s+", "hello world"), "ssss") # \t\n\r\f\v 任何空白符
# print(re.findall("\S+", "hello world")) # [^\t\n\r\f\v]
# print(re.findall("\w+", "hello world")) # [0-9a-zA-Z_]
# print(re.findall("\W", "hello world")) # [^0-9a-zA-Z_]
# print(re.findall("\b", "hello world")) # 空格,&,#
#
# print(re.findall(r"I\b", "hello I am world")) #
# print(re.findall("I\\b", "hello I am world")) #
# # print(re.findall("c\\\f", r"abcde\fgh"))
# # | 或
# print(re.findall("gh|f", "abcde|fgh"))
# # () 分组
# print(re.findall("(abf)*", r"abfabfabfh"))
#
# print(re.findall("(?P<name>\w+)", r"abfabfabfh"))
#
# # search 找到第一个就返回一个对象(需要用group取出),,findall找到所有满足的结果放入列表
#
# print(re.search("\d+","23414afdfasf324fa"))
# print(re.search("\d+","23414afdfasf324fa").group())
# # 分组命名
# print(re.search("(?P<name>[a-z]+)(?P<age>\d+)","23414alex324fa").group("name","age"))
#
# # match 成功返回对象,失败啥也不返回
# print(re.match("\d+", "24dsd143f"))
#
# # split 分割
# print(re.split(" ", "hello abc asf"))
# print(re.split("[ |]", "hello abc|asf"))
# print(re.split("[ab]", "hebllo abc|asf"))
# # ["he","llo abc|asf"]->["he",“llo ","bc|asf"]->
# # ["he","llo "," ","c|asf"]->["he","llo "," ","c|","sf"],
# print(re.split("[ab]", "abc"))
# # 替换
# print(re.sub("\d+","A","dsfaf123aasf42112dfa"))
#
# print(re.subn("\d+","A","dsfaf123aasf42112dfa"))
#
# # 规则,书写规则
# com = re.compile("\d+")
# str1 = "qfaqs234rer1344"
# print(com.findall(str1))
#
# com = re.compile("\d")
# print(com.findall(str1))
# # 迭代器
# ite = com.finditer(str1)
# # next(ite)
# print(ite)
# # ?: 去除优先级
# print(re.findall("www\.(baidu|163)\.com","www.163.com")) # 163
# print(re.findall("www\.(?:baidu|163)\.com","www.163.com")) # www.163.com
# print(re.search("abc|bcd", "abc")) # search
# print(re.search("a(bc)|bcd", "abc").group())
# "\(9[^()]+\)" print(re.findall("(abc)+", "abcabcabc")) # 给整体添加匹配
print(re.findall("abc+", "abcccabcabcfadfabc")) # ?:去除优先级,给c添加重复匹配
最新文章
- ZOJ Problem Set - 1392 The Hardest Problem Ever
- Cloneable接口和Object的clone()方法
- 2.1 CMMI2级——7个PA简述
- libswscale图像格式转换与放大缩小
- Java基础知识强化之多线程笔记02:多线程之 面试题(常问)
- 多个DIV让float:left属性,最后一个DIV填满剩余的部分
- 根据input 标签取value属性的值
- Visual Studio 2013 新增web项目IIS Express的64位版 转载来源http://www.cnblogs.com/jianyus/p/3524335.html
- SQL游标使用及实例
- hdu5860 Death Sequence
- javaMail邮件发送功能(多收件人,多抄送人,多密送人,多附件)
- Win10 将slim加入PYTHONPYTH
- 解决linux下svn update 产生Node remains in conflict的问题
- plsql 工具怎样导出 oracle 表结构
- 设计模式【转自JackFrost的博客】
- mycat工作原理
- 渲染Web视图
- Hive 常用优化参数
- centos shell 编程-通过端口号kill对应的进程
- 批处理学习笔记3 - 变量声明和goto代替while循环
热门文章
- 交换机安全学习笔记 第四章 VLAN
- C++ day01 预备知识、C++综述、教材、推荐阅读。
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
- x系统清理/tmp/文件夹的原理
- ArcGIS 在VS2010中 ESRI.ArcGIS.SOESupport.dll 无法正常加载的处理
- java8之stream和lambda表达式
- SAP发布wbservice,如果有权限管控的话,需要给这个webservice加权限
- 关于strcpy的安全函数的选择
- 关于 i++ 和 ++ i
- mydql 设置充许远程链接