Python match 将括号内外的数据分别取出来

i = R003(201000)

描述:将i分成两部分“R003”和“201000”

import re
i = R003(201000)
num = re.match(r'(.*?)\((.*?)\)(.*?)',i).group(2)
name = re.match(r'(.*?)\((.*?)\)(.*?)',i).group(1)
print(num,name)

(.*?)\((.*?)\)(.*?)

其中(.*?)为最小匹配 \(\)分别表示以(和)作为分隔来取字符串

re.match(r'(.*?)\((.*?)\)(.*?)',i)意思是分成两部分,括号外group(1)=R003和括号内group(2)=201000

match.group()返回匹配对象的一个或多个分组。

match.group(0)(或match.group())表示匹配的所有字段

猜你喜欢

转载自blog.csdn.net/u013155359/article/details/81224855
今日推荐