
正则表达式的学习(2)一些干货
发布日期:2021-05-07 15:03:44
浏览次数:25
分类:精选文章
本文共 2988 字,大约阅读时间需要 9 分钟。
标题正则表达式的学习(2)一些干货
在Python这门语言中完全支持正则表达式,正则作为一个工具毋庸置疑,功能是十分强大的,那么,在深入学习正则前,我想,看一看Python的库re是一个比较好的主意。
首先,打开IDE pycharm,新建任意一个Python文件,导入re库。`按住ctrl键点击re这个字母,打开一个名称为re.py的文件。#Secret Labs' Regular Expression Engine#re-compatible interface for the sre matching engine# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.## This version of the SRE library can be redistributed under CNRI's# Python 1.6 license. For any other use, please contact Secret Labs# AB (info@pythonware.com
以上是源码的开始几行注释,意思是这个正则表达式的引擎是由一个名字叫秘密实验室的工作组AB开发的,从Python1.6开始提供支持,如有其它用途,请联系AB,然后一个邮箱。
第十七行,问题来了。r"""Support for regular expressions (RE).#这个意思是正则支持r‘’字符串,也就是原生字符串类型,#说直白点,意思就是使用r“string”,string内的转义字符不进行#转义,例如:a=r'\\abc//\[abc]//\\{}'和a='\\abc//\#[abc]//\\{}' 是两个不同的字符串,推荐使用加r下面是解释re的使用范围包括编码问题,This module provides regular expression matching operations similar tothose found in Perl. It supports both 8-bit and Unicode strings; boththe pattern and the strings being processed can contain null bytes andcharacters outside the US ASCII range.意思可以用在perl,也支持 8-bit和Unicode编码以及空字符串和us ASCII下面是解释re可以支持的特殊字符以及大部分普通字符Regular expressions can contain both special and ordinary characters.Most ordinary characters, like "A", "a", or "0", are the simplestregular expressions; they simply match themselves. You canconcatenate ordinary characters, so last matches the string 'last'.举例如,“A”,"a",或者“0”,这是最简单的正则表达式下面介绍正则中的特殊字符The special characters are: "." Matches any character except a newline. "^" Matches the start of the string. "$" Matches the end of the string or just before the newline at the end of the string. "*" Matches 0 or more (greedy) repetitions of the preceding RE. Greedy means that it will match as many repetitions as possible. "+" Matches 1 or more (greedy) repetitions of the preceding RE. "?" Matches 0 or 1 (greedy) of the preceding RE. *?,+?,?? Non-greedy versions of the previous three special characters. { m,n} Matches from m to n repetitions of the preceding RE. { m,n}? Non-greedy version of the above. "\\" Either escapes special characters or signals a special sequence. [] Indicates a set of characters. A "^" as the first character indicates a complementing set. "|" A|B, creates an RE that will match either A or B. (...) Matches the RE inside the parentheses. The contents can be retrieved or matched later in the string. (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below). (?:...) Non-grouping version of regular parentheses. (?P...) The substring matched by the group is accessible by name. (?P=name) Matches the text matched earlier by the group named name. (?#...) A comment; ignored. (?=...) Matches if ... matches next, but doesn't consume the string. (?!...) Matches if ... doesn't match next. (?<=...) Matches if preceded by ... (must be fixed length). (?
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月05日 19时06分55秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
BottomNavigationView控件item多于3个时文字不显示
2021-05-08
函数指针的典型应用-计算函数的定积分(矩形法思想)
2021-05-08
8051单片机(STC89C52)以定时器中断模式实现两倒计时器异步计时
2021-05-08
用 wxPython 打印你的 App
2021-05-08
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
2021-05-08
Linux下安装MySql过程
2021-05-08
android:使用audiotrack 类播放wav文件
2021-05-08
vue通过better-scroll 封装自定义的下拉刷新组件
2021-05-08
android解决:使用多线程和Handler同步更新UI
2021-05-08
vue自定义封装Loading组件
2021-05-08
Element UI 中动态路由的分析及实现
2021-05-08
使用springMVC配置视图管理器后找不到指定的页面
2021-05-08
关于js中对于Promise的深入理解
2021-05-08
杭电 2007 平方和与立方和(输入数据的大小顺序并不能默认)
2021-05-08
十大排序算法之三:插入排序(Python)
2021-05-08
利用Python实现循环队列
2021-05-08
利用递归实现二叉树的前中后序遍历(Python)
2021-05-08
Python刷题输入输出
2021-05-08
冒泡排序又来啦(C/C++版本)
2021-05-08
python负数存储
2021-05-08