首页 > 编程语言 >python---re

python---re

时间:2022-08-21 08:55:49浏览次数:76  
标签:group python str1 --- re print 匹配 gdfd

python---re

python的re模块简单使用
re.findall, re.compile, re.match和re.search

re.findall

这个是最好用的,查找所有符合条件的,返回list,或None

import re

str1 = 'gdfd,good'
results = re.findall(r'g..d', str1)
print(results)

re.compile

编译一个正则表达式,用这个在多次调用正则时可能会快一些,把上面的例子改成使用这样的形式

import re

reobj = re.compile(r'g..d')
str1 = 'gdfd,good'
results = reobj.findall(str1)
print(results)

re.match & re.search

match和search都是匹配一次,match必须从开头匹配

match示例

import re

str1 = 'gdfd,good'
m = re.match(r'gdfd', str1)
if m:
    print(m.group(0))
else:
    print('Not found')
m = re.match(r'good', str1)
if m:
    print(m.group(0))
else:
    print('Not found')

结果:

gdfd
Not found

search示例

import re

str1 = 'gdfd,good'
s = re.search(r'gdfd', str1)
if s:
    print(s.group(0))
else:
    print('Not found')
s = re.search(r'good', str1)
if s:
    print(s.group(0))
else:
    print('Not found')

结果:

gdfd
good

对group的解释

group()和group(0)等价,是正则字符串匹配的所有内容
group(1)指捕获的第一个结果(第一个括号匹配的内容),之后类推

一些正则

有分组但不捕获

(?:hello)

一个名字为the_name的分组

(?P<the_name>hello)

使用已命名的分组

(?P<named_group>cool) (?P=named_group)

简单的ip域名匹配

ip_re = rb'(?P<ip>(?:\d{1,3}\.){3}\d{1,3})'
domain_re = rb'(?P<domain>(?:[-a-zA-Z0-9]+\.)+[a-zA-Z]+)'

更多正则和测试可以用这个网站,超级好用: https://regex101.com/

匹配bytes-like object

有时候需要匹配二进制数据,需要在正则表达式前加 b

# coding:utf8

# TypeError: cannot use a string pattern on a bytes-like object

import re

the_re = rb'Hello'
the_bytes = b' Hello fasdf'
result = re.findall(the_re, the_bytes)
print(result)

flag使用示例

可以给正则字符串添加一些标记,实现不同的功能。

re.I
re.IGNORECASE
进行忽略大小写匹配

re.S
re.DOTALL
让 '.' 特殊字符匹配任何字符,包括换行符;如果没有这个标记,'.' 就匹配除了换行符的其他任意字符。对应内联标记 (?s)

re.X
re.VERBOSE
这个标记允许你编写更具可读性更友好的正则表达式。通过分段和添加注释。空白符号会被忽略。当一个行内有 `#` 不在字符集和转义序列,那么它之后的所有字符都是注释。

一个匹配数字的示例:

.text:00401032 68 62 04 00 00    push    462h
.text:00401037 68 8C 7A 41 00    push    offset _Format  ; "%d\n"
.text:0040103C E8 C5 FF FF FF    call    _printf
.text:00401041 59                pop     ecx
.text:00401042 59                pop     ecx
.text:00401043 33 C0             xor     eax, eax
.text:00401045 C3                retn
import re
import struct

num_re = re.compile(
    rb"""
    \x68(.{4})  # 68 62 04 00 00    push    462h
    \x68.{4}    # 68 8C 7A 41 00    push    offset _Format  ; "%d\n"
    \xE8.{4}    # E8 C5 FF FF FF    call    _printf
    \x59        # 59                pop     ecx
    \x59        # 59                pop     ecx
    \x33\xC0    # 33 C0             xor     eax, eax
    \xC3        # C3                retn
    """,
    re.DOTALL | re.VERBOSE,
)

the_file = open("Hello.exe", "rb")
the_content = the_file.read()
the_file.close()

raw_data_list = num_re.findall(the_content)
for raw_data in raw_data_list:
    num = struct.unpack("<L", raw_data)[0]
    print(hex(num))
# 0x462

参考链接

  1. https://www.runoob.com/python/python-reg-expressions.html
  2. https://docs.python.org/zh-cn/3/library/re.html

2018/11/12

标签:group,python,str1,---,re,print,匹配,gdfd
From: https://www.cnblogs.com/-rvy-/p/16609304.html

相关文章

  • PowerShell教程 - 编程结构(Program Struct)- 第五部分
    更新记录转载请注明出处。2022年8月21日发布。2022年8月18日从笔记迁移到博客。枚举类型定义枚举类型(Defininganenumeration)简单的定义enumMyEnum{Zero......
  • PowerShell教程 - 编程结构(Program Struct)- 第四部分
    更新记录转载请注明出处。2022年8月21日发布。2022年8月18日从笔记迁移到博客。分支结构(Branching)if语句(ifStatement)if(条件){ #code}实例:$userInput=R......
  • linux---man
    linux---man命令man,manuals,手册、说明书,用来查看命令、api的用法对应网址:https://man7.org/linux/man-pages/index.html查看命令例子:#查看kill命令手册,用`m......
  • 关于GreenPlum的构架
    Greenplum数据库是一种大规模并行处理(MPP)数据库服务器,其架构特别针对管理大规模分析型数据仓库以及商业智能工作负载而设计。 MPP(也被称为sharednothing架构)指有两个或......
  • python---struct
    python---structkeywords:structbytespython数据互转https://docs.python.org/3/library/struct.html二进制数据和各种类型数据的转换因为不同平台默认大小端不同,......
  • VMWare workstation 16 pro
    目录NATNetwork了解NAT设备编辑NAT配置文件NAT配置文件中的各个部分Linuxnat.conf文件示例报错errorwhileloadingsharedlibraries:libssl.so.1.1:cannotopenshar......
  • SpringBoot--嵌入式Servlet容器
    一、嵌入式Servlet容器在传统的Web开发中,需要将项目打成war包,在外部配置部署好Tomcat服务器,而这个Tomcat就是Servlet容器,在使用SpringBoot开发时,我们无需再外部......
  • ERROR--Disconnected from the target VM, address : '127.0.0.1:6847' , transport :
    DisconnectedfromthetargetVM,address:'127.0.0.1:6847',transport:'socket'原因分析1服务器采用的Tomcat,编译打包方式未设置war【默认打包方式是jar】......
  • ERROR--Unable to resolve column XXXX
    IDEA报错UnabletoresolvecolumnXXXX解决办法。原因分析IDEA无法完全识别数据库的信息场景在IDEA中的Mybatis需要书写SQL语句时,编译过程会报警告idea......
  • ERROR--"Unable to process Jar entry [module-info.class] from Jar"
    UnabletoprocessJarentry[module-info.class]fromJar原因分析无法从Jar包中处理jar条目[module-info.class]场景启动Idea项目时会出现严重的红色错误......