天天看點

re子產品

#! /usr/bin/python3
# -*-coding:UTF-8-*-

import re

print(re.match("hello","hello my world").span())       #在起始位置比對,隻比對字元串開始部分,失敗就傳回None

print(re.match("hi","hi 你好"))

print(re.search("apple","banana orange apple").span())      #随意位置比對,直到找到比對對象,否則沒有傳回None
print(re.search("banana","banana orange apple").span())     #在起始位置比對

#由于正則會消耗一定的資源,是以要用預編譯
re_phone = re.compile(r'^(\d{3})-(\d{3,8})$')
print(re_phone.search('020-12345678').group())