Re: [問題] 字串處理 - 簡化程式碼
※ 引述《auoauo (1493)》之銘言:
: # 剛好最近在用
: # 針對您的items = '127.0.0.1:443;SSL'這個例子的簡單範例:
: import re
: match = re.search(u'''
: (?P<ip>[\d]+\.[\d]+\.[\d]+\.[\d]+)?
: :
: (?P<port>[\d]+)?
: ;
: (?P<comment>[\S]+)?
: ''', items, re.X)
: if match:
: ip, port, comment = match.group('ip'), match.group('port'), match.group('comment')
分享一個寫法
如果 match 到的東西確定要直接丟出來當變數, 可以這樣寫::
if match:
globals().update(match.groupdict())
畢竟 regexp pattern 都定的那麼完整了, 不濫用一下可惜
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.200.180
推
02/27 10:52, , 1F
02/27 10:52, 1F
→
02/27 11:01, , 2F
02/27 11:01, 2F
→
02/27 16:06, , 3F
02/27 16:06, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):