Re: [問題] 字串處理 - 簡化程式碼

看板Python作者 (PyCon APAC 2014)時間11年前 (2014/02/26 17:08), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串3/3 (看更多)
※ 引述《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
是Taiwan PyCon耶
02/27 10:52, 1F

02/27 11:01, , 2F
It's nice!!
02/27 11:01, 2F

02/27 16:06, , 3F
野生的 PyCon 大大出現了 xddd
02/27 16:06, 3F
文章代碼(AID): #1J3X-48k (Python)
文章代碼(AID): #1J3X-48k (Python)