正则表达式

  特殊字符序列,匹配检索和替换文本

  普通字符 + 特殊字符 + 数量,普通字符用来定边界

更改字符思路

  字符串函数 > 正则 > for循环

元字符  匹配一个字符

  # 元字符大写,一般都是取小写的反

  1. 0~9 整数          \d      取反  \D

import re

example_str = "Beautiful is better than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r"\d", example_str))
print(re.findall(r"\D", example_str))

  2. 字母、数字、下划线       \w      取反  \W

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'\w', example_str))
print(re.findall(r'\W', example_str))

  3. 空白字符(空格、\t、\t、\n)   \s      取反  \S

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'\s', example_str))
print(re.findall(r'\S', example_str))

  4. 字符集中出现任意一个    []    0-9 a-z A-Z  取反  [^]

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'[0-9]', example_str))
print(re.findall(r'[^0-9]', example_str))

  5. 除 \n 之外任意字符

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r".", example_str))

数量词  指定前面一个字符出现次数

  1. 贪婪和非贪婪

    a. 默认情况下是贪婪匹配,尽可能最大匹配直至某个字符不满足条件才会停止(最大满足匹配)

    b. 非贪婪匹配, 在数量词后面加上 ? ,最小满足匹配

    c. 贪婪和非贪婪的使用,是程序引起bug重大原因

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'.*u', example_str))
print(re.findall(r'.*?u', example_str))

  2. 重复指定次数        {n} {n, m}

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'\d{3}', example_str))

  3. 0次和无限多次         *

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'.*', example_str))

  4. 1次和无限多次         +  

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'\d+', example_str))

  5. 0次或1次             ?     使用思路: 去重

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'7896?', example_str))

边界匹配

  1. 从字符串开头匹配 ^

  2. 从字符串结尾匹配 $

正则表达式或关系    | 

  满足 | 左边或者右边的正则表达式

import re

example_str = "Beautiful is better_ than ugly 78966828 $ \r \r\n ^Explicit is better than implicit"

print(re.findall(r'\d+|\w+', example_str))

  () 括号内的正则表达式当作单个字符,并且返回()内正则匹配的内容,可以多个,与关系

Python-正则相关模块-re

  1. 从字符中找到匹配正则的字符 findall()

import re
name = "Hello Python 3.7, 123456789" total = re.findall(r"\d+", name)
print(total)

  2. 替换正则匹配者字符串 sub()

import re

def replace(value):
return str(int(value.group()) + 1) result_str = re.sub(r"\d", replace, name, 0)
print(result_str)

匹配一个中文字符   [\u4E00-\u9FA5]

Python-字符串解析-正则-re的更多相关文章

  1. [转] 强大的python字符串解析

    1.python字符串通常有单引号('...').双引号("...").三引号("""...""")或('''...'' ...

  2. Python字符串解析方法汇总

    Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: ...

  3. python字符串、正则-xdd

    1.分割字符串 str.split(sep,maxsplit) #(分隔符,分几次) 2.合并字符串 str2=string.join(iterable) #str2='@'.join(list1) ...

  4. Python入门 —— 04字符串解析

    字符串 -字符串是 Python 中最常用的数据类型.(可以说是大多数语言都常用) 1. 创建字符串 ( '' 或 "" 和 '''''')(单,双和三引号)(字符串可以为空) - ...

  5. Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组

    Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组 import time dt=time.strptime('2019-08-08 11:32:23', ...

  6. python——字符串 & 正则表达

    raw字符串(原始字符串) 所见即所得,例如r''My's\n'' Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续 ...

  7. python 字符串探讨

    本文内容基于python3 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出.字符串的学习是重点中的重点,这一节将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等.大部 ...

  8. python字符串、字符串处理函数及字符串相关操作

    python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...

  9. python浅谈正则的常用方法

    python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...

  10. Python XML解析(转载)

    Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...

随机推荐

  1. 机器学习 | 深入SVM原理及模型推导(一)

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是机器学习专题的第32篇文章,我们来聊聊SVM. SVM模型大家可能非常熟悉,可能都知道它是面试的常客,经常被问到.它最早诞生于上世纪六 ...

  2. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  3. 使用jQuery设置和获取css样式

  4. Tiled and Unity

    https://www.mapeditor.org https://assetstore.unity.com/packages/tools/integration/tiled-to-unity-172 ...

  5. 2019HNCPC C Distinct Substrings 后缀自动机

    题意 给定一个长度为n字符串,字符集大小为m(1<=n,m<=1e6),求\(\bigoplus_{c = 1}^{m}\left(h(c) \cdot 3^c \bmod (10^9+7 ...

  6. HTML语言基本单词与css基本单词

    DOCTYPE  文档     html 网页     head 头部   body 主体   title 题目   p 段落    color 颜色    style 样式    backgroun ...

  7. vue 多环境打包

    https://cli.vuejs.org/zh/guide/mode-and-env.html#%E6%A8%A1%E5%BC%8F 模式 模式是 Vue CLI 项目中一个重要的概念.默认情况下, ...

  8. Mybatis源码学习第六天(核心流程分析)之Executor分析

    今Executor这个类,Mybatis虽然表面是SqlSession做的增删改查,其实底层统一调用的是Executor这个接口 在这里贴一下Mybatis查询体系结构图 Executor组件分析 E ...

  9. Zookeeper原生客户端

    1.1.1.1. 客户端基本操作 package cn.enjoy.javaapi; import org.apache.zookeeper.*; import java.io.IOException ...

  10. python基础:面向对象

    一.定义 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类:一个种类,一个模型. 对象:指具体的东西,模型造出来的东西叫做对象. 实例:实例和对象是一样的. 实例化:实例化就 ...