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

作者: pycontw (PyCon APAC 2014)   2014-02-27 01:08:17
※ 引述《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 都定的那麼完整了, 不濫用一下可惜
作者: peanut97 (丁丁)   2014-02-27 10:52:00
是Taiwan PyCon耶
作者: auoauo (1493)   2014-02-27 11:01:00
It's nice!!
作者: ccwang002 (亮)   2014-02-27 16:06:00
野生的 PyCon 大大出現了 xddd

Links booklink

Contact Us: admin [ a t ] ucptt.com