Skip to content
Starslayerx edited this page Apr 26, 2021 · 1 revision

数据处理工具

正则表达式

import re
  • Regex对象

    regex = re.compile(r'\d\d\d-\d\d\d\d') # r为原始字符串
  • 使用Match对象,匹配Regex对象

    match = regex.search('some string like 123-1234')

    match为 123-1234

    • 为匹配到为None
  • Match对象的group()方法
    若要将匹配的文本分组,则可以使用括号创建“分组”,例如(\d\d\d-\d\d\d\d)

    regex = re.compile(r'(\d\d\d)-(\d\d\d\d)')
    match = regex.search('my number is 123-1234')

    match.group(1)为 123
    match.group(2)为 1234
    match.group(0)或不加参数为全部文本

    • 若要一次获取所有分组,使用match.groups()方法,将每组放入元祖中返回
  • 使用管道匹配多个分组

    regex = re.compile(r'Spider|Bat')
    match1 = regex.match('Spider and Bat') # match1 = Spider
    match2 = regex.match('Bat and Spider') # match2 = Bat

    优先匹配先遇到的词,然后这个分组成这个词了

  • 使用问号实现可选匹配

    regex = re.compile(r'sweet (black )?choloate')

    以上匹配中,如果有black就会匹配black,没有就会匹配其他部分

  • 使用星号进行0或多次匹配

    regex = re.compile(r'(sweet )*chcolate')

    以上可以匹配到chcolate、sweet chcolate、 sweet sweet sweet chcolate

  • 使用加号匹配1或多次

    regex = re.compile(r'(sweet )+chcolate')

    同星号,只不过不包含0次

  • 使用花括号匹配特定次数

    regex = re.compile(r'(hello ){3,5}')

    以上,只会匹配到hello hello hello 或者5个的 python正则表达式默认为贪心的,在大括号后面加?将其变成非贪心的,例如上式,如果够5个就匹配5个,加上?后就匹配3个了

  • findall()方法

    regex = re.compile(r'\d\d\d-\d\d\d\d')
    match_all = regex.findall('...')

    返回由所有匹配相组成的列表

Clone this wiki locally