2018-07-09正则表达式

跳转到底部

由于新接的项目中要用到大量的正则表达式,所以对正则表达式进行了简单的复习

基础知识回顾:正则表达式是什么

正则表达式也称为匹配[模式]: list ,它由一组具有特定含义的字符串组成,通常用于匹配和替换文本。

回顾re模块:

import re # 导入re模块
dir(re) # 查看re模块的方法和属性
['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'RegexFlag', '
S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE','all', '_builtins
_', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', 'ver
sion
', '_alphanum_bytes', '_alphanum_str', '_cache', '_compile', '_compile_repl', '_expand', '_loc
ale', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'enum', 'error', 'escape', 'findall
', 'finditer', 'fullmatch', 'functools', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_pa
rse', 'sub', 'subn', 'template']

加粗斜体是我们常用几个方法的 (只加粗没有斜体的是因为markdown的书写把魔法方法前边和后边的两个__认成加粗符号了,求大神指点)

match方法:re.match(str_request, str_text )

str_request:匹配条件
str_text:待匹配文本
match方法是匹配以str_request开头的str_text文本中的内容
返回的是一个匹配对象(Match Object)
需要调用group()方法去查看内容

>>> res = re.match("任彦彪","任彦彪真帅111111")
>>> res
<_sre.SRE_Match object; span=(0, 3), match='任彦彪'>
>>> res.group()
'任彦彪'

'任彦彪'
'''

findall方法:re.findall(str_request, str_text)

str_request:匹配条件
str_text:待匹配文本
与match方法的参数相同,但作用不同,findall方法是查询文本中所有符合条件的字符串,并返回一个列表

>>> res = re.findall(r"\d+","1 当前浏览量是8808次,回帖人数是200人")
>>> res
['1', '8808', '200']

既然用到了/d+我们就把元字符给复习一下吧

元字符:单个字符的匹配

代码 说明
. 匹配除换行符(\n)以外的任意字符
\w 匹配大小写字母或数字或下划线或汉字0-9、a-z、A-Z、_(下划线)、汉字和其他国家的语言符号
\W 匹配非字母或数字或下划线或汉字,跟\w正好相反
\s 匹配任意的空白符
\S 匹配任意非空白符
\d 匹配数字
\D 匹配非数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
[] 匹配[]中列举的字符

字符转义

\是字符转义,比如你想匹配.或者*,直接打在条件里是不行的,所以需要用/来转义,/.的意思就是.没有匹配所有的意思了,只是一个. 程序演示如下:

>>> res = re.findall(".","sdsa卡卡..卡卡")
>>> res
['s', 'd', 's', 'a', '卡', '卡', '.', '.', '卡', '卡']
>>> res = re.findall("\.","sdsa卡卡..卡卡")
>>> res
['.', '.']

\同理,就表示一个\,比如想匹配路径,路径都是中间都是\有两个解决方案,方案一:一个\表示一个\,
我们需要两个\,那就是\\
方案二:在匹配条件前边加r r"\"此时\没有转义功能,就单纯表示\(我们都推荐这种!)

>>> res = re.match(r"C:\\","C:\\data\\a.txt")
>>> res
<_sre.SRE_Match object; span=(0, 3), match='C:\\'>
>> res = re.match("C:\\\\","C:\\data\\a.txt")
>>> res
<_sre.SRE_Match object; span=(0, 3), match='C:\\'>
>>> res = re.match("C:\\","C:\\data\\a.txt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\re.py", line 172, in match
    return _compile(pattern, flags).match(string)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 488, in _parse
    sourceget()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 255, in get
    self.__next()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 245, in __next
    self.string, len(self.string) - 1) from None
sre_constants.error: bad escape (end of pattern) at position 2

通过代码演示可以看到,方案一和方案二都能行得通, 我知道最后一句匹配不了,可是最后一个语句为什么会报错我很难受QAQ

匹配重复的内容:

<i id="list">a</i>

代码/用法 说明
* 重复零次或多次
+ 重复一次或多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n次到m次

<div id="jump">跳转到的地方</div>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • python的re模块--细说正则表达式 可能是东半球最详细最全面的re教程,翻译自官方文档,因为官方文档写的是真...
    立而人阅读 23,021评论 4 46
  • Python中的正则表达式(re) import rere.match #从开始位置开始匹配,如果开头没有则无re...
    BigJeffWang阅读 7,193评论 0 99
  • python:正则表达式 什么是正则表达式 正则表达式也叫做匹配模式(Pattern),它由一组具有特定含义的字符...
    清清子衿木子水心阅读 521评论 0 2
  • #首先,python中的正则表达式大致分为以下几部分: 元字符 模式 函数 re 内置对象用法 分组用法 环视用法...
    mapuboy阅读 1,656评论 0 51
  • 寂寞冬寒夜敛声, 谁知月下小窗明。 迷心乱弄千丝绪, 异日行装欲哪程。
    糊涂印象阅读 134评论 0 0