看板 Python 關於我們 聯絡資訊
※ 引述《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
peanut97:是Taiwan PyCon耶 02/27 10:52
auoauo:It's nice!! 02/27 11:01
ccwang002:野生的 PyCon 大大出現了 xddd 02/27 16:06