美文网首页
Python3正则表达式常用方法

Python3正则表达式常用方法

作者: 冲锋丘丘人 | 来源:发表于2019-04-26 14:56 被阅读0次

前言

re.findall(),re.search()re.match()是Python正则表达式中常用的方法,re.search()re.match()类似,此次主要介绍re.findall()re.search()
re.match() 尝试从字符串的起始位置匹配一个模式,匹配成功方法返回一个匹配的对象,否则返回None。
re.search() 扫描整个字符串并返回第一个成功的匹配,匹配成功方法返回一个匹配的对象,否则返回None。
re.findall()在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
注意: match 和 search 是匹配一次, findall 匹配所有。
实例:

import re,json
dd = {
"data": {
        "XB-afea46d26a0f0b2d016a0f516710007c": [
            "17"
        ]
    },
    "url":"<html><h1>what the fuck!</h1></html>",
    "time":"243dsfaefa2019-04-03 18:12:45dafjk3k345345kadasjfk",
    "phone":"2004-959-559"
}
st = json.dumps(dd)
pattern1 = r'-[0-9]+'
print(re.search(pattern1,st).group())
print(re.findall(pattern1,st))

输出结果

-04
['-04', '-03', '-959', '-559']

正则表达式带有一个捕获组

findall返回该捕获组内正则表达式所匹配的字符的列表,search则会匹配整个正则表达式并返回第一个
实例

import re,json
dd = {
"data": {
        "XB-afea46d26a0f0b2d016a0f516710007c": [
            "17"
        ]
    },
    "url":"<html><h1>what the fuck!</h1></html>",
    "time":"243dsfaefa2019-04-03 18:12:45dafjk3k345345kadasjfk",
    "phone":"2004-959-559"
}
st = json.dumps(dd)
pattern2 = r'-(\d+)'
print(re.search(pattern2,st))
print(re.search(pattern2,st).group(0))
print(re.search(pattern2,st).group(1))
print(re.findall(pattern2,st))

输出

<re.Match object; span=(128, 131), match='-04'>
-04
04
['04', '03', '959', '559']

正则表达式带有多个个捕获组

findall返回多个捕获组内正则表达式所匹配的字符的元组的列表,search则会匹配整个正则表达式并返回第一个
实例

import re,json
dd = {
"data": {
        "XB-afea46d26a0f0b2d016a0f516710007c": [
            "17"
        ]
    },
    "url":"<html><h1>what the fuck!</h1></html>",
    "time":"243dsfaefa2019-04-03 18:12:45dafjk3k345345kadasjfk",
    "phone":"2004-959-559"
}
st = json.dumps(dd)
pattern3 = r'(?P<year>\d{4})-([0-9]{2})-(\d\d)'
print(re.search(pattern3,st))
print(re.search(pattern3,st).group(0))
print(re.search(pattern3,st).group('year'))
print(re.search(pattern3,st).group(2))
print(re.search(pattern3,st).group(3))
print(re.findall(pattern3,st))

结果

<re.Match object; span=(124, 134), match='2019-04-03'>
2019-04-03
2019
04
03
[('2019', '04', '03')]

相关文章

  • 正则表达式汇总

    1.正则表达式一般命名为regex 2.JS正则表达式常用的方法 方法举例 3.常用限定符号 4.常用元字符 5....

  • 正则表达式

    什么是正则表达式?如何创建正则表达式正则表达式常用的方法字符串中的正则表达式常用的正则表达式假设用户需要在HTML...

  • 【day 2】python 正则表达式入门篇 -1

    标签: python 正则表达式 工具 站长工具(在线验证正则) 常用字符 学习资料 廖雪峰python3菜鸟教程...

  • Python之time模块

    前言 time模块是Python3的内置模块,常用方法如下 实例 输出

  • python创建与删除文件

    Python3 OS 文件/目录方法常用的方法如下: os.access(path, mode)检验权限模式 os...

  • 爬虫面试基础整理

    常用网络数据爬取方法urllib正则表达式Beautiful SoupSeleniumScrapyLxml 常见的...

  • pyhon 高级教程笔记

    python3正则表达式: http://www.runoob.com/python3/python3-reg-e...

  • 正则表达式与方法

    正则表达式---常用符号 正则表达式--常用函数 正则表达式--常用技巧 代码: 正则表达式的应用举例 1、使用f...

  • Python 正则表达式——re模块介绍

    Python 正则表达式 re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.ma...

  • Python 正则表达式——re模块介绍

    Python 正则表达式 re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.ma...

网友评论

      本文标题:Python3正则表达式常用方法

      本文链接:https://www.haomeiwen.com/subject/lgnfnqtx.html