没有用过的一个函数是re.split()
, 平时都是用str.split()
# -*- coding: utf-8 -*-
import re
# match: 从字符串开始进行匹配
x=re.match(r"(\d)\d*","123 1234 2345")
print(type(x))
print(x.group()) # 123
print(x.groups()) # ()匹配的内容 ('1',)
print(x.group(0)) # group()==group(0) #是整个字符串
print(x.group(1)) # groups()==[group(1), group(2),...] #是每个括号的匹配情况
p=re.compile(r"(\d)\d*")
x=p.match("123 1234 12345")
print(type(x))
print(x.group()) # 123
print(x.groups()) # ('1',)
x=re.match(r"(\d)\d*","a123 1234 2345")
print(x) # None
#search 和 match 差不多,但是不是从第一个字符匹配
x=re.search(r"(\d)\d*","123 1234 2345")
print(type(x))
print(x.group()) # 123
print(x.groups()) # ()匹配的内容 ('1',)
p=re.compile(r"(\d)\d*")
x=p.search("123 1234 12345")
print(type(x))
print(x.group()) # 123
print(x.groups()) # ('1',)
x=re.search(r"(\d)\d*","a123 1234 2345")
print(type(x))
print(x.group()) # 123
print(x.groups()) # ('1',)
#findall: 找到所有匹配,返回list,没有()相当于在外面包一个(),如果()>=2 则返回list的每一项是tuple
x=re.findall(r"(\d)\d*","123 1234 2345")
print(type(x)) # list
print(x) #['1', '1', '2']
x=re.findall(r"\d\d*","123 1234 2345")
print(type(x)) # list
print(x) #['123', '1234', '2345']
p=re.compile(r"\d\d*")
x=p.findall("123 1234 2345")
print(type(x)) # list
print(x) #['123', '1234', '2345']
x=re.findall(r"\d\d*","a123 1234 2345")
print(type(x)) # list
print(x) #['123', '1234', '2345']
#sub: 替换, 下面的例子是把数字的首位替换成0
x=re.sub(r"\d(\d*)",r"0\1","123 1234 2345")
print(type(x)) # str
print(x) #023 0234 0345
p=re.compile(r"\d(\d*)")
x=p.sub(r"0\1","123 1234 2345")
print(type(x)) # list
print(x) #023 0234 0345
x=re.sub(r"\d(\d*)",r"0\1","a123 1234 2345")
print(type(x)) # str
print(x) #a023 0234 0345
# '|' 符号的优先级应该很低,下面的情况可以不用加(),相对来说'*' '+' '?' 如果用在字符串上是需要加()的
x=re.match(r"(123)|(456)","456")
print(x.group()) # 456
print(x.groups()) # (None, '456')
x=re.match(r"123|456","456")
print(x.group()) # 456
print(x.groups()) # ()