2020-04-17重新编辑学习

正则的应用场景

校验用户输入格式,例如是否为邮箱
mysql中使用正则表达式
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
问题:mysql中使用正则表达式会导致查询效率下降吗?原因是什么
ElasticSearch中使用正则表达式

GET /_search
{"query": {"regexp": {"user": {"value": "k.*y","flags" : "ALL","max_determinized_states": 10000,"rewrite": "constant_score"}}}
}

MongoDB 正则表达式
db.posts.find({post_text:{$regex:"hello[A-Z]"}})

正则概念

Regular Expression 中文正则表达式,目前最常见的应用是作为一种匹配模式。
一个正则表达式通常被称为一个模式(pattern),为用来描述或者匹配一系列符合某个句法规则的字符串

正则表达式的发展历史

对于我来说,我曾使用较多的场景是通过grep这个命令。

提起正则表达式,其实这里面还要说到一个人Kenneth Lane Thompson,
这尊神几乎以一己之力奠定了人类计算机操作系统的基础。

关于grep的历史可以参考这篇文章

Python里面的正则表达式

greedy 贪婪,可以理解为尽可能多的
Non-greedy 非贪婪,可以理解为尽可能少的

re.py读一遍,比任何书籍的收获都要大。

Support for regular expressions (RE).This module provides regular expression matching operations similar to
those found in Perl.  It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves.  You can
concatenate ordinary characters, so last matches the string 'last'.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 atthe 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<name>...) 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).(?<!...) Matches if not preceded by ... (must be fixed length).(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,the (optional) no pattern otherwise.The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.\number  Matches the contents of the group of the same number.\A       Matches only at the start of the string.\Z       Matches only at the end of the string.\b       Matches the empty string, but only at the start or end of a word.\B       Matches the empty string, but not at the start or end of a word.\d       Matches any decimal digit; equivalent to the set [0-9] inbytes patterns or string patterns with the ASCII flag.In string patterns without the ASCII flag, it will match the wholerange of Unicode digits.\D       Matches any non-digit character; equivalent to [^\d].\s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] inbytes patterns or string patterns with the ASCII flag.In string patterns without the ASCII flag, it will match the wholerange of Unicode whitespace characters.\S       Matches any non-whitespace character; equivalent to [^\s].\w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]in bytes patterns or string patterns with the ASCII flag.In string patterns without the ASCII flag, it will match therange of Unicode alphanumeric characters (letters plus digitsplus underscore).With LOCALE, it will match the set [0-9_] plus characters definedas letters for the current locale.\W       Matches the complement of \w.\\       Matches a literal backslash.This module exports the following functions:match     Match a regular expression pattern to the beginning of a string.fullmatch Match a regular expression pattern to all of a string.search    Search a string for the presence of a pattern.sub       Substitute occurrences of a pattern found in a string.subn      Same as sub, but also return the number of substitutions made.split     Split a string by the occurrences of a pattern.findall   Find all occurrences of a pattern in a string.finditer  Return an iterator yielding a Match object for each match.compile   Compile a pattern into a Pattern object.purge     Clear the regular expression cache.escape    Backslash all non-alphanumerics in a string.Some of the functions in this module takes flags as optional parameters:A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \Dmatch the corresponding ASCII character categories(rather than the whole Unicode categories, which is thedefault).For bytes patterns, this flag is the only availablebehaviour and needn't be specified.I  IGNORECASE  Perform case-insensitive matching.L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.M  MULTILINE   "^" matches the beginning of lines (after a newline)as well as the string."$" matches the end of lines (before a newline) as wellas the end of the string.S  DOTALL      "." matches any character at all, including the newline.X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.U  UNICODE     For compatibility only. Ignored for string patterns (itis the default), and forbidden for bytes patterns.This module also defines an exception 'error'.

import

re.compile()
re.search()
re.match()
re.findall()

当pattern用括号进行分组时,第一个括号是group(1),以此类推
.group()
.group(1)
.groups()

python标准库之re相关推荐

  1. Python 标准库之 xml.etree.ElementTree xml解析

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  2. Python标准库介绍

    1. 关于Python标准库 众所周知,Python是一个依赖强大的组件库完成对应功能的语言,为了便捷实现各项功能,前辈大牛们打造了多种多样的工具库公开提供给大众使用,而越来越多的库已经因为使用的广泛 ...

  3. Python标准库queue模块原理浅析

    Python标准库queue模块原理浅析 本文环境python3.5.2 queue模块的实现思路 作为一个线程安全的队列模块,该模块提供了线程安全的一个队列,该队列底层的实现基于Python线程th ...

  4. Python标准库threading模块Condition原理浅析

    Python标准库threading模块Condition原理浅析 本文环境python3.5.2 threading模块Condition的实现思路 在Python的多线程实现过程中,在Linux平 ...

  5. Python标准库asyncio模块基本原理浅析

    Python标准库asyncio模块基本原理浅析 本文环境python3.7.0 asyncio模块的实现思路 当前编程语言都开始在语言层面上,开始简化对异步程序的编程过程,其中Python中也开始了 ...

  6. [python教程入门学习]Python标准库映射类型与可散列数据类型的关系

    本文章向大家介绍Python标准库映射类型与可散列数据类型的关系,主要包括Python标准库映射类型与可散列数据类型的关系使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋 ...

  7. 用于生成随机数的python标准库模块是_17 Python 标准库之 random 模块 - Python 进阶应用教程...

    Python 标准库之 random 模块 随机数是随机产生的数,比如购买彩票,中奖的号码就是随机的.random 库是用于生成随机数的 Python 标准库,random 库提供如下函数: 函数 功 ...

  8. 转Python 标准库 urllib2 的使用细节

    Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库.这里总结了一些 urllib2 库的使用细节. 1 P ...

  9. python标准库很丰富支持多种编程范式_计算机考试简答题

    1 .简述 python 的特点: Python 具有简单易学,免费.开源,可扩展性.可嵌人性和可移植性强,代码规范.代 码具有较好可读性, Python 标准库很丰富,支持命令式编程.面向对象程序设 ...

  10. Python标准库:内置函数dict(mapping, **kwarg)

    Python标准库:内置函数dict(mapping, **kwarg) 本函数是从一个映射函数对象构造一个新字典. 与dict(**kwarg)函数不一样的地方是參数输入是一个映射类型的函数对象,比 ...

最新文章

  1. R语言as.double函数(转化为浮点型)和is.double函数(检验是否为浮点型)实战
  2. Access2007中如何运行SQL执行SQl语句
  3. 【技术综述】深度学习在自然语言处理中的应用发展史
  4. html中属性idx区别,HTML 中的name属性和id属性有什么区别?
  5. java aspectj_Java:AspectJ的异常翻译
  6. 针对数据科学家和数据工程师的4条SQL技巧
  7. java面试题34下面关于程序编译说法正确的是()
  8. 用户登录功能设计思路
  9. 浅谈外网通过反向代理访问内网资源时的权限保护
  10. Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
  11. 使用protobuf_example_addressbook.proto项目时的问题:PROTOBUF_USE_DLLS
  12. eclipse报错:The project was not built due to “Could not delete...”. Fix the problem...
  13. 【蓝桥杯练习--递归】费解的开关
  14. 自动上传本地图片和word图片(word图片需使用从word粘贴功能)
  15. VS Code 中选中英文单词大小写切换快捷键
  16. 英语作文计算机国际会议开幕词,学术会议发言稿 英文(精选多篇)
  17. WebSocket实现在线聊天
  18. Swift 周报 第四期
  19. 在同一个WiFi下的两台电脑,使用webService 和 axis 实现接口调用
  20. TCP第三次握手数据丢失怎么办

热门文章

  1. Linux—vim/vi 翻页跳转命令快捷键
  2. 接口目标 java 1614953528
  3. 标识符的命名规定java 0126
  4. 仓库对象DataSet与小车对象DataAdapter的 关键命令 1201
  5. Timer定时器控件 1130
  6. 综合演练 实现登陆功能 1124
  7. sum() over()函数使用
  8. elasticsearch 去重计数
  9. Java单链表、双端链表、有序链表实现
  10. 《编程珠玑(续)(修订版)》—第2章2.1节Awk中的关联数组