(五)5-3Python正则表达式

news/2024/7/24 6:53:20 标签: python

正则对象的方法

1、match 方法

import  re
reg = re.compile(r'(hello w.*)(hello cn.*)')
a = 'hello world hello cnblogs'
result = reg.match(a)
print(result)

b = "aa" + a
print(b)
result2 = reg.match(b)
print(result2)

运行结果:

<_sre.SRE_Match object at 0x00000000025E21C8>
hello world hello cnblogs
aahello world hello cnblogs
None

2、search方法

import  re
reg = re.compile(r'(hello w.*)(hello cn.*)')
a = 'hello world hello cnblogs'
b = "aa" + a
print(b)
result3 = reg.search(b)
print(result3)
print(result3.group())

运行结果:

aahello world hello cnblogs
<_sre.SRE_Match object at 0x0000000002592140>
hello world hello cnblogs

3、正则对象的split方法

import re
p = re.compile(r'\d+')
print(p.split('one1two2three3four4'))

运行结果:

['one', 'two', 'three', 'four', '']

注:直接把p的正则当成分隔符,然后把最后的字符串用p进行分割,最终返回结果

4、正则对象的findall方法
findall(string[,pos[,endpos]])
搜索string,以列表形式返回全部能匹配的字符串

import re
p = re.compile(r'\d+')
print(p.findall('one1two2three3four4'))

运行结果

['1', '2', '3', '4']

注:findall把匹配的字符串最后一列表的形式返回

5、正则对象的finditer方法
finditer(string[,pos[,endpos]])
搜索string,返回一个顺序访问每一个结果(match对象)的迭代器
import re
p = re.compile(r'\d+')
a_string = 'one1two2three3four4'
for i in p.finditer(a_string):
    # print(i)
    print(type(i))
    print(i.group())

运行结果:

<type '_sre.SRE_Match'>
1
<type '_sre.SRE_Match'>
2
<type '_sre.SRE_Match'>
3
<type '_sre.SRE_Match'>
4

注:p.finditer(a_string)是一个迭代器,返回的每个m都是match对象

6、正则对象的sub方法

sub(pattern, repl, string, count=0)
pattern : 正则中的模式字符串。
repl : 替换的字符串,也可为一个函数。
string : 要被查找替换的原始字符串。
count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。

match匹配对象

import  re
reg = re.compile(r'(?P<tagname>abc)(.*)(?P=tagname)')
result = reg.match('abclfjasdasda234hhkhabc')

print(dir(result))
print(result)
print(result.groups())
print(result.group(2))
print(result.group('tagname'))
print('*'*10)
print(result.groupdict())

运行结果:

['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
<_sre.SRE_Match object at 0x0000000002662140>
('abc', 'lfjasdasda234hhkh')
lfjasdasda234hhkh
abc
**********
{'tagname': 'abc'}

注:
1、 result 由字符串转换成了正则对象
2、 result.groups()是所有的匹配到的数据,每个()是一个元素,最终返回一个tuple
3、 group()可以通过下标(从1开始)的方式访问,也可以通过分组名进行访问
4、 groupdict只能显示有分组名的数据

 

转载于:https://www.cnblogs.com/pythonlx/p/7841277.html


http://www.niftyadmin.cn/n/660699.html

相关文章

用 zsh 配置 shell 终端

用 Zsh 配置 shell 终端 说明 一般的操作系统会自带 bash shell &#xff0c;但是 bash shell 不是很好用&#xff0c;因此决定做一个升级&#xff0c;提高开发效率 1. fish shell 最开始安装的是 fish shell ,这个 shell 的优点就是使用默认配置&#xff0c;就可以满足绝大部…

PM2 使用入门

PM2 使用入门 说明 PM2 是 node 进程管理工具&#xff0c;包括守护进程、监控、日志等一套完整的功能 安装 npm install pm2 -g 命令 首先要有一个可以启动的 node 文件 const http require(http);http .createServer(function(req, res) {res.writeHead(200, { Content-Ty…

idea 自动导入包 快捷键

idea可以自动优化导入包&#xff0c;但是有多个同名的类调用不同的包&#xff0c;必须自己手动AltEnter设置 设置idea导入包 勾选标注 1 选项&#xff0c;IntelliJ IDEA 将在我们书写代码的时候自动帮我们优化导入的包&#xff0c;比如自动去掉一些没有用到的包。 勾选标注 2 选…

ES6+ Proxy与Reflect使用

ES6 Proxy与 Reflect 使用 说明 Proxy 译为 “代理器”&#xff0c;用于修改某些操作的默认行为&#xff0c;可以认为是在目标对象|函数之前架设一层“拦截”&#xff0c;外接的访问&#xff0c;都必须通过这层拦截&#xff0c;这层拦截中可以对外接的访问进行过滤和改写 1. 基…

接口测试框架——第六篇-读Excel封装方法

谢谢小麦苹果的提醒&#xff0c;才发现我借口测试少写了一个文件&#xff0c;今天给大家补上&#xff1a; common->service->excel_case_data.py # coding: utf-8import json import sys import logging import setting import requests from common.module import excel…

关于Swing中JFrame等顶级容器的层次还有设置背景的方式

资料来自&#xff1a;http://blog.csdn.net/qq_32006373/article/details/49659129 http://yuncode.net/code/c_5196327caac3917 在做一个Java大作业&#xff0c;做了一个登录界面后觉得太单调&#xff0c;于是想说加个背景。先是直接弄了个ImageIcon&#xff0c;然后改成Image…

升级 webpack 到 4.x 版本

升级 webpack 到 4.x 版本 说明 webpack 4 可以零配置启动&#xff0c;也支持通过配置文件进行更细致的配置&#xff0c;相比于上一个版本&#xff0c;配置已经简化了很多 webpack 4 引入了 mode 用来设置开发环境&#xff0c;同时也可以简化配置项&#xff0c;不同的 mode 会…

mongoDB 入门使用

MongoDB 入门使用 说明 MongoDB 是一个基于分布式文件存储的数据库&#xff0c;目的是为 WEB 应用提供可扩展的高性能的数据存储解决方案&#xff0c;它支持的数据结构非常松散&#xff0c;是类似 json 的 bson 格式&#xff0c;因此可以存储比较复杂的数据类型。 1. 安装 查看…