• 技术文章 >Python技术 >Python基础教程

    python3 re结合正则表达式如何使用?

    小妮浅浅小妮浅浅2020-11-20 17:01:07原创2098
    大概在十几年前,自动车还是大家比较流行的出门工具。后来人们觉得骑到很远的地方比较费劲,给自行车加上了电瓶,这就成了当时简易的电动车版本。同样的在python3的re模块中,正则表达式经常和re放在一起使用,小编认为两者的关系就类似于自动车和电瓶的组装,大家觉得呢?下面就讲讲结合在一起怎么使用吧。


    1

    2

    3

    4

    5

    6

    7

    8

    # 推荐使用 Python 正则表达式的几个步骤

    import re

    regex = re.compile(r'正则表达式') # 创建一个 Regex 对象,使用 r'' 原始字符串不需要转义

    regex.match() #

    regex.search() # 返回一个 Match 对象,包含被查找字符串中的第一次被匹配的文本

    regex.findall() # 返回一组字符串列表,包含被查找字符串中的所有匹配

    regex.sub()  # 替换字符串,接收两个参数,新字符串和正则表达式

    ...

    简单示例:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    >>> import re

    >>> regex = re.compile(r'\b\w{6}\b') # 匹配6个字符的单词

    >>> regex.search('My phone number is 421-2343-121')

    >>> text = regex.search('My phone number is 421-2343-121')

    >>> text.group()      # 调用 group() 返回结果

    'number'

      

    >>> regex = re.compile(r'0\d{2}-\d{8}|0\d{3}-\d{7}') # 注意分枝条件的使用

    >>> text = regex.search('My phone number is 021-76483929')

    >>> text.group()

    '021-76483929'

    >>> text = regex.search('My phone number is 0132-2384753')

    >>> text.group()

    '0132-2384753'

      

    >>> regex = re.compile(r'(0\d{2})-(\d{8})') # 括号分组的使用

    >>> text = regex.search('My phone number is 032-23847533')

    >>> text.group(0)

    '032-23847533'

    >>> text.group(1)

    '032'

    >>> text.group(2)

    '23847533'

      

    >>> regex = re.compile(r'(0\d{2}-)?(\d{8})') # ?之前的分组表示是可选的分组,如果需要匹配真正的?,就使用转义字符\?

    >>> text = regex.search('My phone number is 032-23847533')

    >>> text.group()

    '032-23847533'

    >>> text = regex.search('My phone number is 23847533')

    >>> text.group()

    '23847533'

      

    >>> regex = re.compile(r'(Py){3,5}') # Python 默认是贪心,尽可能匹配最长的字符串

    >>> text = regex.search('PyPyPyPyPy')

    >>> text.group()

    'PyPyPyPyPy'

    >>> regex = re.compile(r'(Py){3,5}?') # ? 声明非贪心,尽可能匹配最短的字符串

    >>> text = regex.search('PyPyPyPyPy')

    >>> text.group()

    'PyPyPy'


    其它正则规则可自行测试。下面是 Python 正则表达式的常用方法:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    # 这里测试 findall() 以及 sub()

    # findall()

    >>> regex = re.compile(r'0\d{2}-\d{8}|0\d{3}-\d{7}')                      

    >>> regex.findall('Cell: 021-38294729, Work: 0413-3243243')

    ['021-38294729', '0413-3243243']

      

    >>> regex = re.compile(r'Hello \w+')

    >>> regex.sub('Hello Python', 'falkdjfsk Hello c sldfjlksdj Hello java sdfsj')

    'falkdjfsk Hello Python sldfjlksdj Hello Python sdfsj'

    相信通过以上的三段代码,小伙伴们已经初步体会re结合正则表达式的运用了,看不懂的小伙伴多试几遍哦~更多Python学习推荐:PyThon学习网教学中心

    专题推荐:python3re
    上一篇:python3的内置hash函数是什么? 下一篇:怎么使用Python中的hashlib模块?

    相关文章推荐

    • python3 re字符是什么?有什么用?• python3 re闭包操作符是什么?有什么用?• python3 re如何快速编译?• python3 re有哪些返回形式?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网