Python正则表达式Cheat sheet
1.来源:
Python Regular Expressions Cheat Sheet
2.内容:
Special Characters
^ | Matches the expression to its right at the start of a string. It matches every such instance before each \n in the string.
$ | Matches the expression to its left at the end of a string. It matches every such instance before each \n in the string.
. | Matches any character except line terminators like \n.
\ | Escapes special characters or denotes character classes.
A|B | Matches expression A or B. If A is matched first, B is left untried.
+ | Greedily matches the expression to its left 1 or more times.
* | Greedily matches the expression to its left 0 or more times.
? | Greedily matches the expression to its left 0 or 1 times. But if ? is added to qualifiers (+, *, and ? itself) it will perform matches in a non-greedy manner.
{m} | Matches the expression to its left m times, and not less.
{m,n} | Matches the expression to its left m to n times, and not less.
{m,n}? | Matches the expression to its left m times, and ignores n. See ? above.
Character Classes (a.k.a. Special Sequences)
\w | Matches alphanumeric characters, which means a-z, A-Z, and 0-9. It also matches the underscore, _.
\d | Matches digits, which means 0-9.
\D | Matches any non-digits.
\s | Matches whitespace characters, which include the \t, \n, \r, and space characters.
\S | Matches non-whitespace characters.
\b | Matches the boundary (or empty string) at the start and end of a word, that is, between \w and \W.
\B | Matches where \b does not, that is, the boundary of \w characters.
\A | Matches the expression to its right at the absolute start of a string whether in single or multi-line mode.
\Z | Matches the expression to its left at the absolute end of a string whether in single or multi-line mode.
Sets
[ ] | Contains a set of characters to match.
[amk] | Matches either a, m, or k. It does not match amk.
[a-z] | Matches any alphabet from a to z.
[a\-z] | Matches a, -, or z. It matches - because \ escapes it.
[a-] | Matches a or -, because - is not being used to indicate a series of characters.
[-a] | As above, matches a or -.
[a-z0-9] | Matches characters from a to z and also from 0 to 9.
[(+*)] | Special characters become literal inside a set, so this matches (, +, *, and ).
[^ab5] | Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5.
Groups
( ) | Matches the expression inside the parentheses and groups it.
(? ) | Inside parentheses like this, ? acts as an extension notation. Its meaning depends on the character immediately to its right.
(?PAB) | Matches the expression AB, and it can be accessed with the group name.
(?aiLmsux) | Here, a, i, L, m, s, u, and x are flags:
a— Matches ASCII onlyi— Ignore caseL— Locale dependentm— Multi-lines— Matches allu— Matches unicodex— Verbose
(?:A) | Matches the expression as represented by A, but unlike (?PAB), it cannot be retrieved afterwards.
(?#...) | A comment. Contents are for us to read, not for matching.
A(?=B) | Lookahead assertion. This matches the expression A only if it is followed by B.
A(?!B) | Negative lookahead assertion. This matches the expression A only if it is not followed by B.
(?<=B)A | Positive lookbehind assertion. This matches the expression A only if B is immediately to its left. This can only matched fixed length expressions.
(?<!B)A | Negative lookbehind assertion. This matches the expression A only if B is not immediately to its left. This can only matched fixed length expressions.
(?P=name) | Matches the expression matched by an earlier group named “name”.
(...)\1 | The number 1 corresponds to the first group to be matched. If we want to match more instances of the same expresion, simply use its number instead of writing out the whole expression again. We can use from 1 up to 99 such groups and their corresponding numbers.
Popular Python re module Functions
re.findall(A, B) | Matches all instances of an expression A in a string B and returns them in a list.
re.search(A, B) | Matches the first instance of an expression A in a string B, and returns it as a re match object.
re.split(A, B) | Split a string B into a list using the delimiter A.
re.sub(A, B, C) | Replace A with B in the string C.
3.链接:
Python3标准库 正则表达式操作
Python正则表达式Cheat sheet的更多相关文章
- 正则表达式 cheat sheet
- Tools - 速查表与备忘单(Cheat Sheet)
Cheat Sheets Rico's cheatsheets Cheat-Sheets.org Python Python Cheat sheet Python Programming Cheat ...
- XSS (Cross Site Scripting) Prevention Cheat Sheet(XSS防护检查单)
本文是 XSS防御检查单的翻译版本 https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sh ...
- Cheat sheet for Jupyter Notebook
近期,DataCamp发布了jupyter notebook的 cheat sheet,[Python数据之道]第一时间与大家一起来分享下该cheat sheet的内容. 以下是该cheat shee ...
- numpy, pandas, scikit-learn cheat sheet (速查表)
1. scikit-learn cheat sheet 官方链接如下:http://scikit-learn.org/stable/tutorial/machine_learning_map/ Oft ...
- Web前端开发必备手册(Cheat sheet)
转自:http://blog.bingo929.com/cheat-sheets-for-web-develop.html Cheat sheet这个词组如果直译成中文,意思大概是”作弊小抄”之类的词 ...
- Reverse Shell Cheat Sheet
Reverse Shell Cheat Sheet If you're lucky enough to find a command execution vulnerability during a ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- Python正则表达式中的re.S
title: Python正则表达式中的re.S date: 2014-12-21 09:55:54 categories: [Python] tags: [正则表达式,python] --- 在Py ...
随机推荐
- “应用程序无法正常启动(oxc000007b)”解决方案
- 避免在block中循环引用(Retain Cycle in Block)
让我们长话短说.请参阅如下代码: - (IBAction)didTapUploadButton:(id)sender { NSString *clientID = @"YOUR_CLIENT ...
- ORACLE中SID和SERVICE_NAME的区别
先来讲一个小故事,2015年6月份,有个客户迁移了数据库,由单实例数据库变成了RAC.JAVA应用程序出现了无法连接数据库的情况,但是PL/SQL能连接上数据库.由于项目比较庞大,虽然在半夜切换的 ...
- 【GLSL教程】(二)在OpenGL中使用GLSL 【转】
http://blog.csdn.net/racehorse/article/details/6616256 设置GLSL 这一节讲述在OpenGL中配置GLSL,假设你已经写好了顶点shader和像 ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- 【Salvation】——人物角色动画实现
写在前面:这个角色动画主要使用JavaScript编写脚本,在Unity3D游戏引擎的环境中实现. 一.显示角色并实现镜像效果 1.显示贴图: create→cube→修改名称为player,位置归0 ...
- The network connection was lost 文件下载错误提示
假设出现这种错误,可能是模拟器断网,重新启动下模拟器就能够:The network connection was lost
- 如何让DIV居中
总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...
- SharePoint 的PowerShell命令之获取所有网站模版
Get-SPWebTemplate | select Name, Title
- Iptables实现公网IP DNAT/SNAT
Iptables实现NAT是最基本的功能,大部分家用路由都是基于其SNAT方式上网,使用Iptables实现外网DNAT也很简单,不过经常会出现不能正常NAT的现象.以下命令将客户端访问1.1.1.1 ...