1、什么是正则表达式?

  正则表达式,又称规则表达式,是一门小型的语言,通常被用来检索、替换那些符合某个模式(规则)的文本。

2、匹配字符:

  . 匹配除换行符以外的任意字符
  \w 匹配字母或数字或下划线或汉字
  \s 匹配任意的空白符
  \d 匹配数字
  \b 匹配单词的开始或结束
  ^ 匹配字符串的开始
  $ 匹配字符串的结束

3、匹配次数:

  * 重复零次或更多次
  + 重复一次或更多次
  ? 重复零次或一次
  {n} 重复n次
  {n,} 重复n次或更多次
  {n,m} 重复n到m次

4、常用方法

4.1 match

从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None

 match(pattern, string, flags=0)
# pattern: 正则模型
# string : 要匹配的字符串
# falgs : 匹配模式
X VERBOSE Ignore whitespace and comments for nicer looking RE's.
I IGNORECASE Perform case-insensitive matching.
M MULTILINE "^" matches the beginning of lines (after a newline)
as well as the string.
"$" matches the end of lines (before a newline) as well
as the end of the string.
S DOTALL "." matches any character at all, including the newline. A ASCII For string patterns, make \w, \W, \b, \B, \d, \D
match the corresponding ASCII character categories
(rather than the whole Unicode categories, which is the
default).
For bytes patterns, this flag is the only available
behaviour and needn't be specified. L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
U UNICODE For compatibility only. Ignored for string patterns (it
is the default), and forbidden for bytes patterns.

帮助文档

# 无分组
r = re.match("h\w+", origin)
print(r.group()) # 获取匹配到的所有结果
print(r.groups()) # 获取模型中匹配到的分组结果
print(r.groupdict()) # 获取模型中匹配到的分组结果 # 有分组 # 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来) r = re.match("h(\w+).*(?P<name>\d)$", origin)
print(r.group()) # 获取匹配到的所有结果
print(r.groups()) # 获取模型中匹配到的分组结果
print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组 Demo

demo

4.2 search

浏览整个字符串去匹配第一个,未匹配成功返回None

search(pattern, string, flags=0)

        r = re.search("a\w+", origin)
print(r.group()) # 获取匹配到的所有结果
print(r.groups()) # 获取模型中匹配到的分组结果
print(r.groupdict()) # 获取模型中匹配到的分组结果 # 有分组 r = re.search("a(\w+).*(?P<name>\d)$", origin)
print(r.group()) # 获取匹配到的所有结果
print(r.groups()) # 获取模型中匹配到的分组结果
print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

demo

4.3 findall

findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;空的匹配也会包含在结果中

findall(pattern, string, flags=0)

# 无分组
r = re.findall("a\w+",origin)
print(r) # 有分组
origin = "hello alex bcd abcd lge acd 19"
r = re.findall("a((\w*)c)(d)", origin)
print(r)

demo

4.4 sub

# sub,替换匹配成功的指定位置字符串

sub(pattern, repl, string, count=0, flags=0)

# pattern: 正则模型

# repl   : 要替换的字符串或可执行对象

# string : 要匹配的字符串

# count  : 指定匹配个数

# flags  : 匹配模式

# 与分组无关

        origin = "hello alex bcd alex lge alex acd 19"
r = re.sub("a\w+", "", origin, 2)
print(r)

4.5 split

split,根据正则匹配分割字符串

split(pattern, string, maxsplit=0, flags=0)

pattern: 正则模型

string : 要匹配的字符串

maxsplit:指定分割个数

flags  : 匹配模式

        origin = "hello alex bcd alex lge alex acd 19"
r = re.split("alex", origin, 1)
print(r) # 有分组 origin = "hello alex bcd alex lge alex acd 19"
r1 = re.split("(alex)", origin, 1)
print(r1)
r2 = re.split("(al(ex))", origin, 1)
print(r2)

demo

4.6常用正则表达式

IP:
^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$
手机号:
^1[3|4|5|8][0-9]\d{8}$
邮箱:
[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+

5、计算器实例代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re origin = "11+22-(33/3)+4+(12+(66/3))"
# print(eval(origin)) #使用eval 方法 def f1(*args):
'''
计算()里面的内容
:param args:
:return:
'''
return 1 while True:
result = re.split("\(([^()]+)\)",origin,1)
if len(result) == 3:
before,content,after = result
r = f1(content)
new_str = before + str(r) + after
origin = new_str
print(origin)
else:
final = f1(origin)
print(final)
break

demo

最新文章

  1. Xcode LLDB Debug教程
  2. 【Linux】Linux 目录结构
  3. Eclipse的 JSON Edit插件
  4. Nginx reopen reload作用及工作过程
  5. 图论--最近公共祖先问题(LCA)模板
  6. 嵌入式 H264视频通过RTMP直播
  7. oracle中的decode的使用
  8. Gazebo機器人仿真學習探索筆記(一)安裝與使用
  9. java 基础 ----- Arrays 工具类
  10. UVA12253 简单加密法 Simple Encryption
  11. MySQL的随笔
  12. sqlserver清除缓存(转载)
  13. TestNG中DataProvider的用法
  14. Entity Framework在WCF中序列化的问题(转)
  15. Object-C开发之instancetype和id关键字
  16. iOS ---进阶之摇一摇
  17. PHP之mb_strstr使用
  18. 微信 msg_sec_check接口PHP 调用
  19. Hive2.x 版本的安装及配置 以及要注意的事项
  20. Java学习笔记七(目录操作)

热门文章

  1. oracle GROUP BY rollup
  2. django-form and fields validation
  3. vue.js 2.0 学习笔记
  4. 【algorithm】二叉树的遍历
  5. Java 堆内存和栈内存
  6. error: stray &#39;\343&#39; in program 问题解决
  7. bufferedinputStream操作
  8. openssl安装介绍
  9. P2421 A-B数对(增强版)
  10. Django 模型ORM