import repattern = re.compile(r'\d+')print(re.split(pattern,'one1two2three3four4'))import repattern = re.compile(r'\d+')print(re.findall(pattern,'one1two2three3four4'))為什么第一個結果是英文第二個是數字
1 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
你的Python程序輸出結果是對的.
re.split是以re.
compile
中的正則表達式對字符串進行切分.
re.findall是以re.
compile
中的正則表達式對字符串進行匹配.
就拿你的例子來說吧,
re.split是以數字為邊界對字符串
'one1two2three3four4'
進行切分,得到[
'one'
,
'two'
,
'three'
,
'four'
,'']五個字符串
re.findall是匹配字符串
'one1two2three3four4'
中的所有數字,得到[
'1'
,
'2'
,
'3'
,
'4'
]四個數字
添加回答
舉報
0/150
提交
取消